在进行数据传时,我们可能要用到xml来承担
这就需要将获得的xml转换到我们的实体类,或者将我们的实体类序列化为xml传送出去
------------------------------------------------------------------------------------------------------------------------------
//示例类
public class XmlData
{
private string _Bassetcard;
private string _Coperatorname;
private string _Cregister;
private string _Cwarehouseid;
private string _Freplenishflag;
private string _Vbillcode;
private string _Vuserdef10;
public string Bassetcard
{
get { return _Bassetcard; }
set { _Bassetcard = value; }
}
public string Coperatorname
{
get { return _Coperatorname; }
set { _Coperatorname = value; }
}
public string Cregister
{
get { return _Cregister; }
set { _Cregister = value; }
}
public string Cwarehouseid
{
get { return _Cwarehouseid; }
set { _Cwarehouseid = value; }
}
public string Freplenishflag
{
get { return _Freplenishflag; }
set { _Freplenishflag = value; }
}
public string Vbillcode
{
get { return _Vbillcode; }
set { _Vbillcode = value; }
}
public string Vuserdef10
{
get { return _Vuserdef10; }
set { _Vuserdef10 = value; }
}
}
1:从xml到我们的实体类
/// <summary>
/// 这里的xml是我们传送数据时接收的字符串,
/// 这里采用先从本地加载,再从loadmxl来加载
/// </summary>
/// <returns></returns>
public static List<XmlData> GetFromXml()
{
List<XmlData> items = new List<XmlData>();
XmlDocument doc = new XmlDocument();
doc.Load(@"G:\sda.xml");
//获得字符串
//加载文档
//此字符串是我们接收的字符串
string xml = doc.OuterXml;
doc.LoadXml(xml);
XmlNode rootNode = doc.ChildNodes[1];
foreach (XmlNode node in rootNode.ChildNodes)
{
XmlData item = new XmlData();
if (node.NodeType == XmlNodeType.Element)
{
foreach (XmlElement element in node.ChildNodes)
{
switch (element.Attributes["name"].Value)
{
case "bassetcard":
item.Bassetcard = element.Attributes["value"].Value;
break;
case "coperatorname":
item.Coperatorname = element.Attributes["value"].Value;
break;
case "cregister":
item.Cregister = element.Attributes["value"].Value;
break;
case "cwarehouseid":
item.Cwarehouseid = element.Attributes["value"].Value;
break;
case "freplenishflag":
item.Freplenishflag = element.Attributes["value"].Value;
break;
case "vbillcode":
item.Vbillcode = element.Attributes["value"].Value;
break;
case "vuserdef10":
item.Vuserdef10 = element.Attributes["value"].Value;
break;
}
}
items.Add(item);
}
}
return items;
}
2:将我们的实体类转换程xml字符串来传送
/// <summary>
/// 获得要传送的字符串
/// </summary>
/// <returns></returns>
public static string SendToXml()
{
//需要序列化的实体
List<XmlData> dataList = GetFromXml();
XmlDocument doc = new XmlDocument();
//声明
XmlDeclaration xd;
xd = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
XmlElement root = doc.CreateElement("volist");
if (dataList != null)
{
foreach (XmlData item in dataList)
{
XmlElement elenent = doc.CreateElement("vo");
root.AppendChild(elenent);
Entitiytoxml(doc, root, item, elenent);
}
doc.AppendChild(root);
//添加声明
doc.InsertBefore(xd, root);
doc.Save(Console.Out);
}
return doc.OuterXml;
}
/// <summary>
/// 处理结点
/// </summary>
/// <param name="doc">文档</param>
/// <param name="root"></param>
/// <param name="item"></param>
/// <param name="element"></param>
private static void Entitiytoxml(XmlDocument doc, XmlElement root, XmlData item, XmlElement element)
{
System.Reflection.PropertyInfo[] propertyInfo = typeof(XmlData).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
XmlElement xmlElement = null;
foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
{
xmlElement = doc.CreateElement("field");
if (pinfo != null)
{
string name = pinfo.Name;
string value = string.Empty;
if (pinfo.GetValue(item, null) != null)
{
value = pinfo.GetValue(item, null).ToString();
}
//添加属性到field
XmlAttribute attr = doc.CreateAttribute("name");
attr.Value = name;
xmlElement.Attributes.Append(attr);
XmlAttribute attr1 = doc.CreateAttribute("value");
attr1.Value = value;
xmlElement.Attributes.Append(attr1);
XmlAttribute attr2 = doc.CreateAttribute("type");
attr2.Value = pinfo.Name.GetType().ToString();
xmlElement.Attributes.Append(attr2);
element.AppendChild(xmlElement);
}
}
root.AppendChild(element);
}