顯示具有 XML 標籤的文章。 顯示所有文章
顯示具有 XML 標籤的文章。 顯示所有文章

2013年12月13日 星期五

[C#]取得類別(Class)所有的成員(Members),以產生XML為例

前言

工作上寫到很多的XML Element,但看到有些人再寫複雜XML文件時,程式碼還冗長的,譬如以下的XML文件:

image

有些人可能不會想太多,很直覺的寫以下程式:

image

XmlDocument doc = new XmlDocument();
XmlElement company = doc.CreateElement("Company");
doc.AppendChild(company);

XmlElement id = doc.CreateElement("id");
id.InnerText = _Staff.id.ToString();
company.AppendChild(id);

XmlElement StaffName = doc.CreateElement("StaffName");
StaffName.InnerText = _Staff.StaffName.ToString();
company.AppendChild(StaffName);

XmlElement Birthday = doc.CreateElement("Birthday");
Birthday.InnerText = _Staff.Birthday.ToShortDateString();
company.AppendChild(Birthday);

XmlElement weight = doc.CreateElement("Dept");
weight.InnerText = _Staff.weight.ToString();
company.AppendChild(weight);

但根據DRY原則(Don’t repeat youself),如果發現一直在ctrl+v程式碼,一定要跟李組長一樣眉頭一皺,發現專案並不單純…


實做


因目前資料來源習慣都使用Entity Framework (強型別)來操作物件,取代ADO.NET的DataReader or DataTable (弱型別)


而C#能用PropertyInfo來抓取object的所有成員


故我將產生冗長的XmlElement抽出來處理:


private void CreateModelXmlElement(ref XmlDocument doc,ref XmlElement root)
{
Type type = model.GetType();
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
if (propertyInfo.CanRead)
{
XmlElement _element = doc.CreateElement(propertyInfo.Name);
if (propertyInfo.PropertyType == typeof(DateTime))
{
//針對DateTime去做日期格式處理
DateTime value = Convert.ToDateTime(propertyInfo.GetValue(model, null));
_element.InnerText = value.ToShortDateString();
}
else
{
var value = propertyInfo.GetValue(model, null);
_element.InnerText = value.ToString();
}
root.AppendChild(_element);
}
}
}

這樣子就將麻煩的Create XmlElement縮短成以下片段:

XmlDocument doc = new XmlDocument();
XmlElement company = doc.CreateElement("Company");
doc.AppendChild(company);

CreateModelXmlElement(ref doc, ref company);

延伸閱讀


http://msdn.microsoft.com/zh-tw/library/system.reflection.propertyinfo(v=vs.110).aspx


http://stackoverflow.com/questions/3723934/using-propertyinfo-to-find-out-the-property-type


http://stackoverflow.com/questions/957783/loop-through-an-objects-properties-in-c-sharp

2013年9月26日 星期四

[ASP.NET]C#發送XML Request至PHP頁面

需求概述
專案上常常有與公司夥伴的系統做一些資料的交換,在以往我遇過的都是使用.NET 的Web Service走SOAP協定來做,但這次拿到的規格書是要走HTTP POST的方式,並義好XML架構,在加上一些加解密來做異質系統的交換,而對方的系統是用PHP撰寫的,試了好久終於將傳遞與接收完成,趕快紀錄一下 :(

WebRequest XmlRequest = WebRequest.Create("http://xxxxxxx/Service.php");
XmlRequest.Method = "POST";
//XML範例
string XML = @"
    <XML>
        <TEST>
            <eventTime>2013-08-24 08:00:00</eventTime>
        </TEST>
    </XML>";
byte[] byteArray = Encoding.UTF8.GetBytes("temp=" + XML); //temp為參數的名稱
XmlRequest.ContentLength = byteArray.Length;
XmlRequest.ContentType = "application/x-www-form-urlencoded";
Stream RequestStream = XmlRequest.GetRequestStream();
RequestStream.Write(byteArray, 0, byteArray.Length);
RequestStream.Close();
WebResponse response = XmlRequest.GetResponse(); //開始實做傳遞
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
RequestStream = response.GetResponseStream();
StreamReader reader = new StreamReader(RequestStream);
string responseFromServer = reader.ReadToEnd(); //解析對方回傳的XML

Reference
http://stackoverflow.com/questions/6995314/how-to-send-an-xml-from-a-c-sharp-desktop-application-to-a-php-server-script-and PHP Receiver http://docs.php.net/wrappers.php.php