有一个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>
总共约5000个属性,XML中约100000个项目。每件商品约15-20种不同的物业。在每个项目中都有自己的属性集。 item中每个节点的名称是属性id。
如何将其反序列化为类似的内容?
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; }
}
我会感激任何帮助。感谢。
答案 0 :(得分:0)
您可以尝试使用Visual Studio安装附带的xsd.exe工具。请将XML转换为XSD,然后将XSD转换为CS类。它是一个命令行工具。有关该工具的位置,请参阅this。使用以下命令:
xsd myFile.xml / outputdir:myOutputDir(获取xsd文件)
xsd your.xsd / classes(获取cs类)。
以下是我为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;
}
}
}
答案 1 :(得分:0)
我喜欢使用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());
}
}
}