序列化是最好的方法吗?并保持类名相同(事务)可以更改XML文档中的节点名称事务。我在类
中声明了参数public class transaction //Main Class
{
public string propertyID;
public patronInfo TrxPatron;//sub class
public transactioninfo TrxDetails ;// sub class
}
public class patronInfo
{
public string patronID;
}
public class transactioninfo
{
public string Key;
}
static void Main(string[] args)
{
transaction tobj = new transaction();
patronInfo pobj = new patronInfo();
transactioninfo tiobj = new transactioninfo();
tobj.propertyID = "123456";// I add the rest of the values too
string value= SerializeElement("po.xml", tobj);// save to a file
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(value);//I get the xmlfile as a string i convert it into XMl
Console.WriteLine(xdoc.ToString());
Console.Read();
}
// this is my serialization method i use file to avoid compilation error
public static string SerializeElement(string filename,object transactiondetails)
{
XmlSerializer ser = new XmlSerializer(typeof(transaction));
StringWriter sww = new StringWriter();
XmlWriter writer1 = XmlWriter.Create(sww);
ser.Serialize(writer1, transactiondetails);
writer1.Close();
return sww.ToString();
}
答案 0 :(得分:0)
您可以按照与struct
相同的方式序列化class
。
您可以使用XmlRoot
属性更改XML元素名称:
[XmlRoot("MyNewElementName")]
public class transaction //Main Class
{
public string propertyID;
public patronInfo TrxPatron;//sub class
public transactioninfo TrxDetails ;// sub class
}