【问题标题】:Parse JCR XML export in C#在 C# 中解析 JCR XML 导出
【发布时间】:2019-10-14 09:08:10
【问题描述】:

我从一位客户那里收到了一些需要用 C# 处理的 XML 文件。查看结构并进行一些谷歌搜索,似乎该内容已从 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 文件的方式相同。您可以使用 XmlSerializer 或 LINQ to XML。如果要在 Visual Studio 中生成强类型类,请转到 Edit&gt; Paste Special &gt; Paste XML as classes。如果你有一个 XSD,你可以从中生成类

标签: c# xml xmlserializer jcr


【解决方案1】:

下面的代码使用 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());
        }
    }
}

【讨论】:

    【解决方案2】:

    必须创建一个具有相同类型的 XML 的类,可以通过 Visual Studio Edit>Paste Special 来反序列化它。

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

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 2015-06-06
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多