【问题标题】:Read XmlElement and XmlAttribute读取 XmlElement 和 XmlAttribute
【发布时间】:2014-02-26 19:35:00
【问题描述】:

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

如何读取属性&lt;PC&gt;MESSAGE元素(属性太)和URL

【问题讨论】:

  • 使用 Visual Studio 的 XSD.exe 实用程序从您的 xml 中创建 c# 类。

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


【解决方案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; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    相关资源
    最近更新 更多