将XML文件序列化为C#对象

时间:2016-08-11 15:19:30

标签: c# xml

我有以下文件需要序列化到对象:

<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>
  

注意:可以找到完整文件here

如何将此XML表示为C#对象?

到目前为止,我已经尝试了

  • 将特殊粘贴为XML类
  • SimpleXMLToCode

但是我没有给我正确的POCO实体......

我从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;
        }
    }
}

当我使用XmlSerializer (ConstituencyResults) reader.Deserialize(file);时,我得到:

1 个答案:

答案 0 :(得分:4)

“将XML粘贴为类”没有问题。刚刚在我的笔记本电脑上测试过它。

它无法正常工作,因为您忘记关闭results元素。

您的XML必须如下所示:

<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>

修复XML之后,这就是我从“将XML粘贴为类”中获得的:

/// <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;
        }
    }
}