Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XML文件序列化为C#对象_C#_Xml - Fatal编程技术网

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

将XML文件序列化为C#对象,c#,xml,C#,Xml,我有以下文件需要序列化到对象: <constituencyResults> <constituencyResult seqNo="1"> <consituencyId>2</consituencyId> <constituencyName>Aberconwy</constituencyName> <results> <result> &l

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

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

2.
爱贝康威
实验室
8994
33
骗局
7924
29.10
注意:可以找到完整的文件

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

到目前为止我已经试过了

  • 将特殊对象粘贴为XML类
  • SimpleXMLToCode
但也没有给我正确的POCO实体我需要

我从Paste Special获得以下类作为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;
        }
    }
}
//
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类结果
{
私人宪法结果宪法结果宪法结果宪法结果领域;
/// 
公共宪法结果宪法结果宪法结果宪法结果
{
收到
{
返回此.ConstructionResultField;
}
设置
{
this.constructionResultField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类构成结果构成结果
{
专用字节一致性字段;
私有字符串字段;
私人宪法结果宪法结果结果[]结果字段;
私有字节seqNoField;
/// 
公共字节一致性ID
{
收到
{
返回此.consistuencyidfield;
}
设置
{
this.consistuencyidfield=值;
}
}
/// 
公共字符串组成名
{
收到
{
返回此.configurencyNameField;
}
设置
{
this.constructionNameField=值;
}
}
/// 
[System.Xml.Serialization.XmlArrayItemAttribute(“结果”,IsNullable=false)]
公共宪法结果宪法结果结果结果[]结果
{
收到
{
返回此.resultsField;
}
设置
{
this.resultsField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节序号
{
收到
{
返回此.seqNoField;
}
设置
{
this.seqNoField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类组成结果组成结果结果结果
{
私有字符串partyCodeField;
二等兵乌肖特·沃特斯菲尔德;
私有十进制共享字段;
/// 
公共字符串partyCode
{
收到
{
返回此.partyCodeField;
}
设置
{
this.partyCodeField=值;
}
}
/// 
公众投票
{
收到
{
返回这个.votesField;
}
设置
{
this.votesField=值;
}
}
/// 
公开十进制股份
{
收到
{
返回此.shareField;
}
设置
{
this.shareField=值;
}
}
}
当我使用
XmlSerializer
执行
(ConstructencyResults)读取器时。反序列化(文件)我得到:

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

2.
爱贝康威
实验室
8994
33
骗局
7924
29.10
修复XML后,我从“将XML粘贴为类”中得到了以下信息:

//
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类结果
{
私人宪法结果宪法结果宪法结果宪法结果领域;
/// 
公共宪法结果宪法结果宪法结果宪法结果
{
收到
{
返回此.ConstructionResultField;
}
设置
{
this.constructionResultField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类构成结果构成结果
{
专用字节一致性字段;
私有字符串字段;
私人宪法结果宪法结果结果[]结果字段;
私有字节seqNoField;
/// 
公共字节一致性ID
{
收到
{
返回此.consistuencyidfield;
}
设置
{
this.consistuencyidfield=值;
}
}
/// 
公共字符串组成名
{
收到
{
返回此.configurencyNameField;
}
设置
{
this.constructionNameField=值;
}
}
/// 
[System.Xml.Serialization.XmlArrayItemAttribute(“结果”,IsNullable=false)]
公共宪法结果宪法结果结果结果[]结果
{
收到
{
返回此.resultsField;
}
设置
{
this.resultsField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节序号
{
收到
{
返回此.seqNoField
/// <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;
        }
    }
}