我从客户那里收到了一些XML文件,需要用C#处理。查看结构并进行一些谷歌搜索,似乎该内容已从JCR导出(我根本没有使用过,所以我可能是错的)。结构看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<sv:node xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" sv:name="foodtruck-gruener-sepp">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:Event</sv:value>
</sv:property>
<sv:property sv:name="jcr:mixinTypes" sv:type="Name" sv:multiple="true">
<sv:value>mgnl:hasVersion</sv:value>
</sv:property>
<sv:property sv:name="jcr:uuid" sv:type="String">
<sv:value>6371704f-1cc4-4ab9-9298-43c9dd4f79ab</sv:value>
</sv:property>
<sv:property sv:name="name" sv:type="String">
<sv:value>user name</sv:value>
</sv:property>
<sv:property sv:name="email" sv:type="String">
<sv:value>[email protected]</sv:value>
</sv:property>
</sv:node>
我将如何解析它?可以仅将XMLElementAttribute
与XmlSerializer
一起使用吗?
必须创建具有相同类型的XML的类,该类可以通过visual studio Edit生成>选择性粘贴以反序列化。
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.jcp.org/jcr/sv/1.0")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.jcp.org/jcr/sv/1.0", IsNullable = false)]
public partial class node
{
private nodeProperty[] propertyField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("property")]
public nodeProperty[] property
{
get
{
return this.propertyField;
}
set
{
this.propertyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.jcp.org/jcr/sv/1.0")]
public partial class nodeProperty
{
private string valueField;
private string nameField;
private string typeField;
private bool multipleField;
private bool multipleFieldSpecified;
/// <remarks/>
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public bool multiple
{
get
{
return this.multipleField;
}
set
{
this.multipleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool multipleSpecified
{
get
{
return this.multipleFieldSpecified;
}
set
{
this.multipleFieldSpecified = value;
}
}
}
下面的代码使用xml linq并将结果放入字典中。该代码无法解析xml Type属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement node = doc.Root;
XNamespace sv = node.GetNamespaceOfPrefix("sv");
Dictionary<string, string> dict = doc.Descendants(sv + "property")
.GroupBy(x => (string)x.Attribute(sv + "name"), y => (string)y.Element(sv + "value"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}