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?

0

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.