在 C# 中解析 JCR XML 导出

Parse JCR XML export in C#

我从客户那里收到了一些 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>info@somedomain.com</sv:value>
  </sv:property>
</sv:node>

我将如何解析这个?这可以通过仅使用 XMLElementAttributeXmlSerializer 来完成吗?

必须创建与 XML 相同类型的 class,可以通过 visual studio 编辑 > 选择性粘贴来反序列化它。

/// <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());
        }
    }
}