【问题标题】:How to deserialize XML with the different tag names but the same subtags?如何反序列化具有不同标签名称但子标签相同的 XML?
【发布时间】:2015-12-10 14:47:52
【问题描述】:

我有以下 XML:

<Vehicle>
      <Type>
        <ISN>213123214</ISN>
        <Name>ddsd</Name>
      </Type>
      <RegNo>1234</RegNo>
      <Mark>
        <ISN>423434234</ISN>
        <Name>asdasd</Name>
      </Mark>
      <Model>
        <ISN>434234324324</ISN>
        <Name>asddsa</Name>
      </Model>
      <EstimatedPrice>
        <Amount>15000</Amount>
        <AmountPrev />
        <Currency>
          <Code>R</Code>
          <Name>RU</Name>
        </Currency>
      </EstimatedPrice>
</Vehicle>

好吧,我正在尝试使用 C# 将其反序列化为目标对象。 这是我的目标类:

public class Vehicle {
    [XmlElement("Type")]
    public Type Type { get; set; }
    [XmlElement("Mark")]
    public Mark Brand { get; set; }
    [XmlElement("Model")]
    public Mark Brand { get; set; }
    [XmlElement("EstimatedPrice")]
    public Estimation Estimation { get; set; }
}

问题是如何正确反序列化属性Mark、Model和Type?现在我得到的是空对象而不是数据。我试图将“Vehicle”指定为这个子类的根标签,但它没有效果。

附:类 Mark、Model 和 Type 是从基类派生的:

[Serializable]
public class ResponseItem
{
        [XmlElement("ISN")]
        string ISN { get; set; }
        [XmlElement("Name")]
        string Name { get; set; }
}

为了反序列化,我使用 XmlSerializer 类。所有带有 to 子标签或具有唯一名称的子标签的标签都被正确反序列化。

我错过了什么?

谢谢。

【问题讨论】:

  • 显示完整的有效代码。这甚至无法编译 - 请参阅重复的 public Mark Brand { get; set; } 属性。请注意,[Serializable] 与 XML 序列化没有任何关系
  • 好吧,我的错。您可以删除重复的标记。如果你给我一个只有一个领域的例子,这对我来说就足够了。 class Mark : ResponseItem { } class Type : ResponseItem { } 估计也是不必要的,标签“EstimatedPrice”也是如此。
  • 我总是先为 XML 定义 XSD,然后使用 Microsoft 的 XSD.exe 工具生成要序列化和反序列化的类。
  • @user3548735 请编辑您的问题并提供一个最小的、完整的、可验证的示例:stackoverflow.com/help/mcve 这对于 XML 序列化尤其重要 — 魔鬼在细节中,而且通常深埋。

标签: c# xml deserialization xmlserializer


【解决方案1】:

ResponseItem 基类中的属性必须公开

public class ResponseItem
{
    [XmlElement("ISN")]
    public string ISN { get; set; }

    [XmlElement("Name")]
    public string Name { get; set; }
}

XML 序列化程序不会处理非公共属性

【讨论】:

  • 原来如此!
【解决方案2】:

我喜欢逆向工作并创建一个测试序列化程序来验证我的结构。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Vehicle vehicle = new Vehicle() {
                Type = new Type()
                {
                    ISN = "213123214",
                    Name = "ddsd"
                },
                RegNo = 1234,
                Brand = new Mark()
                {
                    ISN = "423434234",
                    Name = "asdasd"
                },
                Model = new Mark()
                {
                    ISN = "434234324324",
                    Name = "asddsa"
                },
                estimation = new Estimation()
                {
                    Amount = 15000,
                    AmountPrev = null,
                    currency = new Currency()
                    {
                        Code = "R",
                        Name = "RU"
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(Vehicle));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, vehicle);
            writer.Flush();
            writer.Close();
            writer.Dispose();
        }
    }
    [XmlRoot("Vehicle")]
    public class Vehicle
    {
        [XmlElement("Type")]
        public Type Type { get; set; }
        [XmlElement("Mark")]
        public Mark Brand { get; set; }
        [XmlElement("Model")]
        public Mark Model { get; set; }
        [XmlElement("RegNo")]
        public int RegNo { get; set; }
        [XmlElement("EstimatedPrice")]
        public Estimation estimation { get; set; }
    }
    [XmlRoot("EstimationPrice")]
    public class Estimation
    {

        [XmlElement("Amount")]
        public int Amount { get; set; }
        [XmlElement("AmountPrev")]
        public int? AmountPrev { get; set; }
        [XmlElement("Currency")]
        public Currency currency { get; set; }
    }
    [XmlRoot("Currency")]
    public class Currency
    {
        [XmlElement("Code")]
        public string Code { get; set; }
        [XmlElement("Name")]
        public string Name { get; set; }
    }


    [XmlRoot("Type")]
    public class Type : ResponseItem
    {
    }
    [XmlRoot("Mark")]
    public class Mark : ResponseItem
    {
    }

    [XmlRoot("ResponseItem")]
    public class ResponseItem
    {
        [XmlElement("ISN")]
        public string ISN { get; set; }
        [XmlElement("Name")]
        public string Name { get; set; }
    }
}
​

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2014-01-18
    相关资源
    最近更新 更多