【问题标题】:Deserialize multiple XML tags to single C# object将多个 XML 标记反序列化为单个 C# 对象
【发布时间】:2016-10-13 07:44:35
【问题描述】:

我的类结构如下

   public class Common
    {
        public int price { get; set; }
        public string color { get; set; }
    }
    public class SampleProduct:Common
    {
        public string sample1 { get; set; }
        public string sample2 { get; set; }
        public string sample3 { get; set; }
    }

我有如下的 XML 文件

<ConfigData>
  <Common>
    <price>1234</price>
    <color>pink</color>    
  </Common>
  <SampleProduct>
    <sample1>new</sample1>
    <sample2>new</sample2>
    <sample3>new123</sample3>
  </SampleProduct>
</ConfigData>

现在我想将完整的 XML 数据反序列化为 SampleProduct 对象(单个对象)。我可以将 XML 数据反序列化为不同的对象,但不能反序列化为单个对象。请帮忙。

【问题讨论】:

  • 您自己创建 XML 文件还是在其他地方提供?
  • 您只需要创建一个新的类名“ConfigData”并在该类中堆肥Common 和SampleProduct。使用 ConfigData 而不是反序列化
  • 我建议进行编辑以修改类结构,所以您在寻找这个吗?

标签: c# xml serialization xml-deserialization


【解决方案1】:

如果你自己创建 XML 就这样做:

<ConfigData>
  <SampleProduct>
    <price>1234</price>
    <color>pink</color>    
    <sample1>new</sample1>
    <sample2>new</sample2>
    <sample3>new123</sample3>
  </SampleProduct>
</ConfigData>

否则,如果您从其他来源获取 XML,请按照 Kayani 在他的评论中建议的操作:

public class ConfigData
{
    public Common { get; set; }
    public SampleProduct { get; set; }
}

但不要忘记以这种方式创建的 SampleProduct 没有“价格”和“颜色”属性,这些应该使用创建的 Common 实例进行设置

【讨论】:

    【解决方案2】:

    这是使用您提供的 XML 的完整解决方案。

        public static void Main(string[] args)
        {
            DeserializeXml(@"C:\sample.xml");
        }
        public static void DeserializeXml(string xmlPath)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(ConfigData));
                using (var xmlFile = new FileStream(xmlPath, FileMode.Open))
                {
                    var configDataOSinglebject = (ConfigData)xmlSerializer.Deserialize(xmlFile);
                    xmlFile.Close();
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException("Something went wrong while interpreting the xml file: " + e.Message);
            }
        }
    
        [XmlRoot("ConfigData")]
        public class ConfigData
        {
            [XmlElement("Common")]
            public Common Common { get; set; }
    
            [XmlElement("SampleProduct")]
            public SampleProduct XSampleProduct { get; set; }
        }
    
        public class Common
        {
            [XmlElement("price")]
            public string Price { get; set; }
    
            [XmlElement("color")]
            public string Color { get; set; }
        }
    
        public class SampleProduct
        {
            [XmlElement("sample1")]
            public string Sample1 { get; set; }
    
            [XmlElement("sample2")]
            public string Sample2 { get; set; }
    
            [XmlElement("sample3")]
            public string Sample3 { get; set; }
        }
    

    这将为您提供一个包含所有您需要的元素的对象。

    【讨论】:

    • configDataOSinglebject 变量为您提供对象。我忘了在那里添加评论。
    猜你喜欢
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多