public class XmlUtility
{
/// <summary>
/// 将自定义对象序列化为XML字符串
/// </summary>
/// <param name="myObject">自定义对象实体</param>
/// <returns>序列化后的XML字符串</returns>
public static string SerializeToXml<T>(T myObject)
{
if (myObject != null)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
writer.Formatting = Formatting.None;//缩进
xs.Serialize(writer, myObject);
stream.Position = 0;
StringBuilder sb = new StringBuilder();
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
sb.Append(line);
}
reader.Close();
}
writer.Close();
return sb.ToString();
}
return string.Empty;
}
/// <summary>
/// 将XML字符串反序列化为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="xml">XML字符</param>
/// <returns></returns>
public static T DeserializeToObject<T>(string xml)
{
T myObject;
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringReader reader = new StringReader(xml);
myObject = (T)serializer.Deserialize(reader);
reader.Close();
return myObject;
}
}
XML Sample - 1
<message>
<header transactionID="1234567890">
</header>
<body>
<cart ID="pre_1000" serial="27301 * -AIW - 1511 - 0124"/>
<operation operator="Tom" timestamp="2008-07-17T15:26:59+05:30" type="1"/>
<products>
<product mixID="AUD-PRD1"/>
<product mixID="AUD-PRD5"/>
</products>
<material id="ABC0011"/>
<position p1="40010R" p2="-1" p3="10" p4="1"/>
</body>
</message>
对应的Object
[Serializable]
public class message
{
public object header { get; set; }
public bodyC body = new bodyC();
[Serializable]
public class bodyC
{
[XmlElementAttribute("cart")]
public object cart { get; set; }
[XmlElementAttribute("operation")]
public object operation { get; set; }
[XmlArrayAttribute("products")]
public product[] products { get; set; }
[XmlRootAttribute("MyProduct")]
public class product
{
[XmlAttribute("mixID")]
public string mixID { get; set; }
}
[XmlElementAttribute("material")]
public materialC material { get; set; }
[Serializable]
public class materialC
{
[XmlAttribute("id")]
public string id { get; set; }
}
[XmlElementAttribute("position")]
public object position { get; set; }
}
}
调用实例
string XMLString = XmlUtility.SerializeToXml(_SocketXML_590Req);
XmlFactory590Req.message _SocketXML_590Req = XmlUtility.DeserializeToObject<XmlFactory590Req.message>(XMLString);
XML Sample - 2
<root>
<Header>
<SystemName>OTHERSYSTEM</SystemName>
<SystemVersion>1.00</SystemVersion>
<SessionId>29961</SessionId>
<CommandName>ProgramDataSend</CommandName>
</Header>
<ProgramDataSend>
<Element>
<Date>2020/07/26,02:20:29</Date>
<MCNo>1</MCNo>
</Element>
<Element>
<Date>2020/07/26,02:20:29</Date>
<MCNo>1</MCNo>
</Element>
</ProgramDataSend>
</root>
对应的Object
public class ProgramDataSendXml
{
[Serializable]
public class root
{
public headerC Header = new headerC();
public ProgramDataSendC ProgramDataSend { get; set; }
public root()
{
Header.CommandName = "ProgramDataSend";
}
}
public class ProgramDataSendC
{
[XmlElementAttribute("Element")]
public List<Element> ElementList { get; set; }
[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute("Element", IsNullable = false)]
public class Element {
[XmlElementAttribute("Date")]
public string Date { get; set; }
[XmlElementAttribute("MCNo")]
public string MCNo { get; set; }
}
}
}