读取XmlElement和XmlAttribute

时间:2014-02-03 10:47:45

标签: c# xml windows-phone-8 deserialization

反序列化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

1 个答案:

答案 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; }
}