0

How do I (de)serialize the following xml element:

<OtherInfo name=\"Some Name\" type=\"text\">Hello World!!!\n</OtherInfo>

I define the following class to take care of the attributes:

[Serializable]
public class OtherInfo
{
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
    public OtherInfo() { }
    public OtherInfo(string name, string type) => (Name, Type) = (name, type);
}

But how do I handle the property value?

Kok How Teh
  • 3,298
  • 6
  • 47
  • 85

1 Answers1

0

Simply add a new property to your class and decorate it with the XmlText attribute. Here a quick link to the documentation.

[Serializable]
public class OtherInfo
{
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }

    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }

    [XmlText]
    public string Value { get; set; }

    public OtherInfo() { }

    public OtherInfo(string name, string type, string value) => (Name, Type, Value) = (name, type, value);
}
Maik Hasler
  • 1,064
  • 6
  • 36