user1909412
user1909412

Reputation: 87

Read XmlElement and XmlAttribute

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>

Class:

[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
}

Method:

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);
    }
}

How to read the attribute <PC>, and MESSAGE element (attribute too) and URL?

Upvotes: 0

Views: 393

Answers (1)

AbinZZ
AbinZZ

Reputation: 795

Add

    [XmlElement("PC")]
    public PC pc { get; set; }

And create class for PC ,

[XmlRoot("PET")]
public class PC
{
    [XmlElement("MESSAGE")]
    public string MES { get; set; }

    [XmlElement("URL")]
    public string url { get; set; }
}

Upvotes: 1

Related Questions