0

序列化是最好的方法吗?并且保持类名相同(事务)我可以更改 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();
}
4

1 回答 1

0
  1. 您可以struct以与class.

  2. XmlRoot您可以使用属性更改 XML 元素名称:

    [XmlRoot("MyNewElementName")]
    public  class transaction //Main Class
    {            
        public string propertyID;                  
        public patronInfo TrxPatron;//sub class
        public transactioninfo TrxDetails ;// sub class
    }
    
于 2013-10-24T18:47:34.957 回答