1

I'm stumped on how to deserialize the following XML into entities that I've created:

 <values totalcount="576">
      <version>3</version>
      <item>
         <datetime>2/22/2016 8:35:00 PM - 8:40:00 PM</datetime>
         <value channel="Outside" channelid="4">10.0000</value>
      </item>
      <item>
         <datetime>2/22/2016 8:40:00 PM - 8:45:00 PM</datetime>
         <value channel="Inside" channelid="2"/>
      </item>
   </values>

These are the classes I've used. When I deserialize, the ValueItems list is created properly with the correct number of items and I get correct TotalCount and Version values but each ValueItem has default values for its members instead of the expected values:

public class Values
    {
        [XmlAttribute(AttributeName = "totalcount")]
        public int TotalCount { get; set; }

        [XmlElement(ElementName = "version")]
        public string Version { get; set; }

        [XmlElement(ElementName ="item")]
        public List<ValueItem> ValueItems { get; set; }
    }

  public class ValueItem
    {
        [XmlElement(ElementName = "datetime")]
        public string DateTime { get; set; }

        [XmlElement(ElementName="value")]
        public SensorValue Value { get; set; }
    }

public class SensorValue
    {
        [XmlAttribute(AttributeName = "channel")]
        public string Channel { get; set; }

        [XmlAttribute(AttributeName = "channelid")]
        public string ChannelId { get; set; }

        public string Value { get; set; }
    }

I've tried decorating ValueItems with XmlArrayItem("item"). I've tried XmlArrayItem along with XmlArray. I've tried decorating the ValueItem class with XmlType("item").

Any ideas? The MSDN docs on using XmlAttributes aren't exactly comprehensive.

2 Answers 2

0

You can just use Visual Studio's built in XML to classes to get this from your XML:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class values {

    private byte versionField;

    private valuesItem[] itemField;

    private ushort totalcountField;

    /// <remarks/>
    public byte version {
        get {
            return this.versionField;
        }
        set {
            this.versionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("item")]
    public valuesItem[] item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort totalcount {
        get {
            return this.totalcountField;
        }
        set {
            this.totalcountField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class valuesItem {

    private string datetimeField;

    private valuesItemValue valueField;

    /// <remarks/>
    public string datetime {
        get {
            return this.datetimeField;
        }
        set {
            this.datetimeField = value;
        }
    }

    /// <remarks/>
    public valuesItemValue value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class valuesItemValue {

    private string channelField;

    private byte channelidField;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string channel {
        get {
            return this.channelField;
        }
        set {
            this.channelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte channelid {
        get {
            return this.channelidField;
        }
        set {
            this.channelidField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Exception thrown when deserializing: Cannot include anonymous type 'Test.Values' when I added the XmlType attribute with Anonymous = true.
@Canoehead how are you deserializing it? It worked for me.
0

Your only problem sees like with the Value property in the SensorValue calss, the rest is working fine.

To get the element values you need to use XmlText attribute. So if you just add that it should work fine.

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

The snippet below displays the values in the sample XML:

string xml = File.ReadAllText("XMLFile1.xml");
using (StringReader reader = new StringReader(xml))
{
    var serializer = new XmlSerializer(typeof(Values), new XmlRootAttribute("values") );
    var result = serializer.Deserialize(reader) as Values;

    result.ValueItems.ForEach(v => Console.WriteLine(v.Value.Value));
}

Output:

10.0000

(Second value is null as it wasn't provided in the XML)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.