将复杂的 XML 反序列化为 C# 对象

发布于 2025-01-03 09:00:04 字数 1628 浏览 0 评论 0原文

我有这种格式的 XML -

<Areas>
  <Area>
    <Property Name="Test11">a1</Property>
    <Property Name="Test12">a2</Property>
    <Property Name="Test13">a3</Property>
    <Property Name="Test14">a4</Property>
    <Property Name="Test15">a5</Property>
  </Area>
  <Area>
    <Property Name="Test21">b1</Property>
    <Property Name="Test22">b2</Property>
    <Property Name="Test23">b3</Property>
    <Property Name="Test24">b4</Property>
    <Property Name="Test25">b5</Property>
  </Area>
</Areas>

我使用 Microsoft 提供的 xsd.exe 生成了类 -

[Serializable()]
    public partial class Areas
    {
        [XmlArrayItem("Property", typeof(AreasAreaProperty))]
        public AreasAreaProperty[][] Area { get; set; }
    }

    [Serializable()]
    public partial class AreasAreaProperty
    {
        [XmlAttribute()]
        public string Name { get; set; }

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

反序列化代码是 -

private void Deserialize()
        {
            XmlSerializer xs = new XmlSerializer(typeof(Areas));
            FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
            XmlReader xr = new XmlTextReader(fs);
            Areas a = (Areas)xs.Deserialize(xr);
            fs.Close();
        }

但在反序列化时,我收到此错误 - 无法将类型“AreasAreaProperty[]”转换为“AreasAreaProperty” 我在创建 XMLSerializer 对象时收到此错误。

怎么解决这个问题呢??提前致谢..

I have XML in this format -

<Areas>
  <Area>
    <Property Name="Test11">a1</Property>
    <Property Name="Test12">a2</Property>
    <Property Name="Test13">a3</Property>
    <Property Name="Test14">a4</Property>
    <Property Name="Test15">a5</Property>
  </Area>
  <Area>
    <Property Name="Test21">b1</Property>
    <Property Name="Test22">b2</Property>
    <Property Name="Test23">b3</Property>
    <Property Name="Test24">b4</Property>
    <Property Name="Test25">b5</Property>
  </Area>
</Areas>

I generated the class using xsd.exe provided by Microsoft as -

[Serializable()]
    public partial class Areas
    {
        [XmlArrayItem("Property", typeof(AreasAreaProperty))]
        public AreasAreaProperty[][] Area { get; set; }
    }

    [Serializable()]
    public partial class AreasAreaProperty
    {
        [XmlAttribute()]
        public string Name { get; set; }

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

Code for deserializing is -

private void Deserialize()
        {
            XmlSerializer xs = new XmlSerializer(typeof(Areas));
            FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
            XmlReader xr = new XmlTextReader(fs);
            Areas a = (Areas)xs.Deserialize(xr);
            fs.Close();
        }

But at the time of deserilaization, i am getting this error -
Cannot convert type 'AreasAreaProperty[]' to 'AreasAreaProperty'
I am getting this error at the time of creation of object of XMLSerializer.

How to solve this issue?? Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌︼了一个春 2025-01-10 09:00:04

我想我以前见过这个。 XSD.exe 并不完美,因此您需要对结果进行一些修改。在以下代码中,在包含 [][] 的最后一行中,删除其中一个 [],使其成为“public AreasAreaProperty[] Area...”

[Serializable()]
public partial class Areas
{
    [XmlArrayItem("Property", typeof(AreasAreaProperty))]
    public AreasAreaProperty[][] Area { get; set; }

I think I've seen this before. XSD.exe isn't perfect and so you need to tinker with the results a little bit. In the following code, on the last line where you have the [][], remove one of the [] so that it's "public AreasAreaProperty[] Area..."

[Serializable()]
public partial class Areas
{
    [XmlArrayItem("Property", typeof(AreasAreaProperty))]
    public AreasAreaProperty[][] Area { get; set; }
喜你已久 2025-01-10 09:00:04

我过去也遇到过类似的麻烦,看看这些问题的答案:

如果您了解您的架构,您应该尝试将其添加到 xsd,而不是将所有事情留给 xsd.exe 工具。

I've had similar troubles in the past, have a look at the answers to these:

If you have knowledge about your schema you should try and add it to the xsd and not leave everything up to the xsd.exe tool.

青丝拂面 2025-01-10 09:00:04

您的根元素是.Deserialize() 方法的第四行不应该是

    Areas a = (Areas)xs.Deserialize(xr); 

由于

    Area a = (Area)xs.Deserialize(xr); 

.

Shouldn't the fourth line of your Deserialize() method be

    Areas a = (Areas)xs.Deserialize(xr); 

instead of

    Area a = (Area)xs.Deserialize(xr); 

since your root element is .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文