反序列化XML,XMLElement,XMLAttribute
XML:
<PET>
<RES>Correct</RES>
<PC version="1.1">
<MESSAGE conf="1">SMS</DESC>
<URL>www.google.com</URL>
</PC>
<PRU>200</PRU>
</PET>
类别:
[XmlRoot("PET")]
public class Prueba
{
[XmlElement("RES")]
public string Res { get; set; }
[XmlElement("PRU")]
public string Pru { get; set; }
//PC Attribute
//MESSAGE element AND Attribute
//URL element
}
方法:
public void Prueba()
{
Prueba p = new Prueba();
XmlSerializer serializer = new XmlSerializer(p.GetType());
using (StreamReader reader = new StreamReader("Repositories/Local/Prueba.xml"))
{
p = (Prueba)serializer.Deserialize(reader);
}
}
如何阅读属性<PC>
和MESSAGE
元素(属性)和URL
?
答案 0 :(得分:1)
添加
[XmlElement("PC")]
public PC pc { get; set; }
为PC创建课程,
[XmlRoot("PET")]
public class PC
{
[XmlElement("MESSAGE")]
public string MES { get; set; }
[XmlElement("URL")]
public string url { get; set; }
}