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

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

Note: the full file can be found here

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

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

  • 特殊粘贴为 XML 类
  • 简单的XMLToCode

但都没有给我所需的正确 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); 我得到:

最佳答案

“Paste XML as Classes”没有问题。刚刚在我的笔记本电脑上测试过。

它无法工作,因为您忘记关闭 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;
        }
    }
}

关于c# - 将 XML 文件序列化为 C# 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900327/

相关文章:

c# - 禁用 WPF 窗口标题栏中的关闭按钮 (C#)

javascript - 过滤列表为空

java - Spring Jaxb2 : How to append batch data to XML file with no reading it to memory?

Python:使用 ElementTree 更新 XML 文件,同时尽可能保留布局

c++ - 使用 QDomDocument 时报告详细的 XML 错误

C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?

c# - 来自 native 应用程序的 CoCreateInstance C# COM 组件找不到引用

c# - 防止 API 发生重大更改

c++ - 使用 Xerces-C++ 解析递归 XML 模式 (XSD) 时出现段错误

XML SAX 处理程序中逻辑的 Java 帮助