Davy C
Davy C

Reputation: 649

XML Deserialisation of XMLElement with value specified

I'm sure this is simple, but I can't find the answer. What would be the correct Class syntax for this part of an XML file?

enter image description here

I have got this far, but seem to be missing something, pardon the pun, key! I feel I'm approaching it in the wrong way, but I can't seem to find an example code that matches an XML that has the value inside the element.

        [Serializable, XmlRoot("Keys")]
    public class Keys
    {
        [XmlElement("Key")]
        public Key Key { get; set; }          
    }

    [Serializable, XmlRoot("Key")]
    public class Key
    {
        [XmlAttribute("TYPE")]
        public string Type { get; set; } 

    }

Upvotes: 0

Views: 43

Answers (1)

TyCobb
TyCobb

Reputation: 9089

I think you are looking for the XmlTextAttribute which will get you the number. Also I think you want a collection of keys in which case, we need to declare it as a collection.

[XmlRoot("Keys")]
public class Keys
{
    [XmlElement("Key")]
    public List<Key> Items { get; set; }          
}

public class Key
{
    [XmlAttribute("TYPE")]
    public string Type { get; set; } 

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

Another way to do it if Keys is not your actual root object, you can use XmlArrayAttribute and XmlArrayItemAttribute

public class MyObject
{
    [XmlArray("Keys")]
    [XmlArrayItem("Key")]
    public List<Key> Keys {get;set;}
}

public class Key
{
    [XmlAttribute("TYPE")]
    public string Type { get; set; } 

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

Upvotes: 1

Related Questions