简体   繁体   English

将具有随机属性集的XML反序列化到对象列表中

[英]Deserialization of an XML with random set of properties into list of objects

There is an xml file: 有一个xml文件:

<xmlRoot>
  <properties>
    <property id = "p45663">property title</property>
    <property id = "p00765">property title</property>
    <property id = "p10431">property title</property>
    <property id = "p08332">property title</property>
    <property id = "p00005">property title</property>         
  </properies>
  <items>
    <item id = "111222">
      <p00001>some value</p00001>
      <p22345>some value</p22345>
      <p05589>some value</p05589>
    </item>
    <item id = "333444">
      <p99323>some value</p99323>
      <p03345>some value</p03345>
      <p07741>some value</p07741>
    </item>
    <item id = "555666">
      <p49113>some value</p49113>
      <p03345>some value</p03345>
      <p00532>some value</p00532>
    </item>
  </items>
</xmlRoot>

Total ~5000 properties, ~100000 items in XML. 总共约5000个属性,XML中约有100000个项目。 About 15-20 various properties per each item. 每个项目约有15-20种不同的属性。 In each item its own set of properties. 在每个项目中都有自己的属性集。 Name of the each node in item is a property id. 项目中每个节点的名称是属性ID。

How can i deserialize it into something like this? 我怎样才能反序列化成这样的东西?

public class xmlDoc
{
  [XmlAttribute("id")]
  public string id { get; set; }
  public List<xmlProp> properties { get; set; }
}

public class xmlProp
{
  public string propertyID { get; set; }
  public string propertyValue { get; set; }
}

I'll be grateful for any help. 我将不胜感激。 Thanks. 谢谢。

You can try using the xsd.exe tool that comes with visual studio installation. 您可以尝试使用Visual Studio安装随附的xsd.exe工具。 Please convert the XML to XSD and then XSD to a CS class. 请先将XML转换为XSD,然后再将XSD转换为CS类。 Its a command line tool. 它是一个命令行工具。 Please refer to this for the location of the tool. 请参阅以获取工具的位置。 Use the below Commands: 使用以下命令:

xsd myFile.xml /outputdir:myOutputDir (to get the xsd file) xsd myFile.xml / outputdir:myOutputDir(获取xsd文件)

xsd your.xsd /classes (to get your cs class). xsd your.xsd / classes(获取CS类)。

Below is the code I generated for your xml file: 以下是我为您的xml文件生成的代码:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.6.1055.0.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class xmlRoot {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("items", typeof(xmlRootItems), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("properties", typeof(xmlRootProperties), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class xmlRootItems {

    private xmlRootItemsItem[] itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public xmlRootItemsItem[] item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class xmlRootItemsItem {

    private string p49113Field;

    private string p99323Field;

    private string p03345Field;

    private string p00532Field;

    private string p07741Field;

    private string p00001Field;

    private string p22345Field;

    private string p05589Field;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p49113 {
        get {
            return this.p49113Field;
        }
        set {
            this.p49113Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p99323 {
        get {
            return this.p99323Field;
        }
        set {
            this.p99323Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p03345 {
        get {
            return this.p03345Field;
        }
        set {
            this.p03345Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p00532 {
        get {
            return this.p00532Field;
        }
        set {
            this.p00532Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p07741 {
        get {
            return this.p07741Field;
        }
        set {
            this.p07741Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p00001 {
        get {
            return this.p00001Field;
        }
        set {
            this.p00001Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p22345 {
        get {
            return this.p22345Field;
        }
        set {
            this.p22345Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string p05589 {
        get {
            return this.p05589Field;
        }
        set {
            this.p05589Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class xmlRootProperties {

    private xmlRootPropertiesProperty[] propertyField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("property", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public xmlRootPropertiesProperty[] property {
        get {
            return this.propertyField;
        }
        set {
            this.propertyField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class xmlRootPropertiesProperty {

    private string idField;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

I like using Dictionary along with the xml linq 我喜欢将Dictionary和xml linq一起使用

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

namespace ConsoleApplication25
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
       static void Main(string[] args)
        {
           XDocument doc = XDocument.Load(FILENAME);
           XmlDoc xmlDoc = new XmlDoc(doc.Root);


        }
    }
    public class XmlDoc
    {
        public Dictionary<string,string> properties { get; set; }
        public Dictionary<string, Dictionary<string,string>> items { get; set; }

        public XmlDoc(XElement root)
        {
            properties = root.Descendants("property")
                .GroupBy(x => (string)x.Attribute("id"), y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            items = root.Descendants("item")
                .GroupBy(x => (string)x.Attribute("id"), y =>
                    y.Elements().GroupBy(a => a.Name.LocalName, b => (string)b)
                    .ToDictionary(a => a.Key, b => b.FirstOrDefault()))
                    .ToDictionary(x => x.Key, y => y.First());


        }
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM