1

I have the following file I need to serialise to objects:

<constituencyResults>
  <constituencyResult seqNo="1">
    <consituencyId>2</consituencyId>
    <constituencyName>Aberconwy</constituencyName>
    <results>
        <result>
          <partyCode>LAB</partyCode>
          <votes>8994</votes>
          <share>33.00</share>
        </result>
        <result>
          <partyCode>CON</partyCode>
          <votes>7924</votes>
          <share>29.10</share>
        </result>
    </results>
  </constituencyResult>
</constituencyResults>

Note: the full file can be found here

How do I represent this XML as C# objects?

So far I've tried

  • Paste Special As XML Classes
  • SimpleXMLToCode

But neither give me the correct POCO entities I need....

I get the following classes from Paste Special As XML Classes:

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

    private constituencyResultsConstituencyResult constituencyResultField;

    /// <remarks/>
    public constituencyResultsConstituencyResult constituencyResult
    {
        get
        {
            return this.constituencyResultField;
        }
        set
        {
            this.constituencyResultField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class constituencyResultsConstituencyResult
{

    private byte consituencyIdField;

    private string constituencyNameField;

    private constituencyResultsConstituencyResultResult[] resultsField;

    private byte seqNoField;

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

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

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("result", IsNullable = false)]
    public constituencyResultsConstituencyResultResult[] results
    {
        get
        {
            return this.resultsField;
        }
        set
        {
            this.resultsField = value;
        }
    }

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

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class constituencyResultsConstituencyResultResult
{

    private string partyCodeField;

    private ushort votesField;

    private decimal shareField;

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

    /// <remarks/>
    public ushort votes
    {
        get
        {
            return this.votesField;
        }
        set
        {
            this.votesField = value;
        }
    }

    /// <remarks/>
    public decimal share
    {
        get
        {
            return this.shareField;
        }
        set
        {
            this.shareField = value;
        }
    }
}

When I use an XmlSerializer doing (ConstituencyResults) reader.Deserialize(file); I get:

Vinyl Warmth
  • 2,226
  • 3
  • 25
  • 50

1 Answers1

4

There is no problem with "Paste XML as Classes". Just tested it on my laptop.

It failed to work because you forget to close results element.

Your XML must look like this :

<constituencyResults>
  <constituencyResult seqNo="1">
    <consituencyId>2</consituencyId>
    <constituencyName>Aberconwy</constituencyName>
    <results>
          <result>
            <partyCode>LAB</partyCode>
            <votes>8994</votes>
            <share>33.00</share>
          </result>
          <result>
            <partyCode>CON</partyCode>
            <votes>7924</votes>
            <share>29.10</share>
          </result>
        </results> <!-- Your forget to close the results element -->
    </constituencyResult>
</constituencyResults>

After fixing your XML, this is what I get from "Paste XML as Classes":

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

    private constituencyResultsConstituencyResult constituencyResultField;

    /// <remarks/>
    public constituencyResultsConstituencyResult constituencyResult
    {
        get
        {
            return this.constituencyResultField;
        }
        set
        {
            this.constituencyResultField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class constituencyResultsConstituencyResult
{

    private byte consituencyIdField;

    private string constituencyNameField;

    private constituencyResultsConstituencyResultResult[] resultsField;

    private byte seqNoField;

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

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

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("result", IsNullable = false)]
    public constituencyResultsConstituencyResultResult[] results
    {
        get
        {
            return this.resultsField;
        }
        set
        {
            this.resultsField = value;
        }
    }

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

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class constituencyResultsConstituencyResultResult
{

    private string partyCodeField;

    private ushort votesField;

    private decimal shareField;

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

    /// <remarks/>
    public ushort votes
    {
        get
        {
            return this.votesField;
        }
        set
        {
            this.votesField = value;
        }
    }

    /// <remarks/>
    public decimal share
    {
        get
        {
            return this.shareField;
        }
        set
        {
            this.shareField = value;
        }
    }
}
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • When I use those classes I get `System.InvalidOperationException` " was not expected." – Vinyl Warmth Aug 11 '16 at 15:51
  • 1
    You must put the correct XML you're using. there is no `xmlns` in your question. – CodeNotFound Aug 11 '16 at 15:54
  • I've just checked & there is no mention of `xmlns` in the source. Yes, I made a mistake leaving out `` - that was an error on my part – Vinyl Warmth Aug 11 '16 at 15:56
  • 1
    When did you get the exception ? When generating the class from the XML ? Or When deserializing your data from the XML ? If it's the latter one then you must create a new thread and I will answer to it because it is another issue. – CodeNotFound Aug 11 '16 at 15:58
  • I get the exception when I try doing this with an `XmlSerializer`: `(ConstituencyResults) reader.Deserialize(file);` Thanks for your help – Vinyl Warmth Aug 11 '16 at 16:00
  • Yep. That is what I think :) It is another issue that is different to the initial problem. Consider creating another thread and put the code you're using to deserialize your data. – CodeNotFound Aug 11 '16 at 16:04
  • What should my question be? I will create a question now under `C#` and `XML` again... – Vinyl Warmth Aug 11 '16 at 16:06
  • The title should be => `[the exception type] occured while deserailizing XML file' The in your question you put the sample XML and the C# code you're using. Don't forget to add the exact message of your exception – CodeNotFound Aug 11 '16 at 16:09
  • I've created the new thread here: http://stackoverflow.com/questions/38901440/systeminvalidoperationexception-when-trying-to-deserialize-xml-to-c-sharp – Vinyl Warmth Aug 11 '16 at 17:09