Linq to XML或XPath从特定XML节点获取数据 [英] Linq to XML or XPath to get the data from a specific XML node

查看:90
本文介绍了Linq to XML或XPath从特定XML节点获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我们有一个大的XML文件(> 27MB)需要解析。我们已将XML文件反序列化为XmlData对象。来自XML的数据完美地保存在这个XmlData对象中的类和对象的适当结构中。



现在,这里的目标是从中获取特定的节点数据基于Selected节点路径的XmlData。我相信这可以使用Linq to XML查询[或] XPath来完成。

但是,我不喜欢Linq到XML,因为在这种情况下我必须为特定节点编写特定查询,我可能需要硬编码特定节点名称。由于复杂性和非常灵活的XML数据,这将是艰难的。



对于XPath,我认为我们只需提供Selected节点路径,它将收集相应的数据。

例如。如果我需要在DOC-REVISIONS下获取数据,那么我将只发送Node路径并在Obj变量中检索其下的所有数据。



var Obj = Xpath(/ node / path)[PS:plz忽略语法]

这个变量我要用于我的申请。



请有人指导我如何实现这个目标。



下面提到的是代码段和XML片段,供您参考:

Hello friends,
We have a big XML file (>27MB) to be parsed. We had deserialized the XML file into a XmlData object. The data from the XML is perfectly saved in this XmlData object in a proper structure of classes and objects.

Now, the goal here is to get the specific node data from the XmlData based on the Selected Node Path. I believe this can be done using Linq to XML query [Or] XPath.
But, I dont prefer Linq to XML because in this case i have to write specific query for specific node and i may have to hardcode the Specific Node names. This will be tough due to the complexity and imense XML data.

In case of XPath, i think we just need to provide the Selected node path and it will collect the corresponding data under it.
For eg. If i need to get the data under DOC-REVISIONS, then i will just send the Node path and retrieve all the data under it in Obj variable.

var Obj = Xpath (/node/path) [PS: plz ignore the syntax]
And this variable i want to use in my application.

Please can somebody guide me how i can achieve this.

Below mentioned is the code snippet and XML snippet, for your reference:

public void CreateDeserializedXmlObject(string strSrcFilename)
        {
            try
            {
                //Using this object we can access the different classes & objects created from that XML

                XmlSerializer deserializer = new XmlSerializer(typeof(ABC));
                TextReader reader = new StreamReader(strSrcFilename);
                object obj = deserializer.Deserialize(reader);
                XmlData = (ABC)obj; // XmlData will have all the parsed data in form of Objects
                    reader.Close();
            }
            catch (Exception ex)
            {}
        }




<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<ABC xmlns="http://www.ABC.net/schema/ABC/r1.0">
    <CATEGORY>TestReport</CATEGORY>
    <ADMIN-DATA>
        <LANGUAGE>DE</LANGUAGE>
        <USED-LANGUAGES>
            <L-10 xml:space="preserve" L="EN">EN</L-10>
        </USED-LANGUAGES>
        <DOC-REVISIONS>
            <DOC-REVISION>
                <REVISION-LABEL>1.0.0.1</REVISION-LABEL>
                <STATE>under_develpment</STATE>
                <TEAM-MEMBER-REF DEST="TEAM-MEMBER">/EBCG/rm</TEAM-MEMBER-REF>
                <DATE>2015-03-27</DATE>
            </DOC-REVISION>
            <DOC-REVISION>
                <REVISION-LABEL>1.0.0.0</REVISION-LABEL>
                <STATE>under_develpment</STATE>
                <TEAM-MEMBER-REF DEST="TEAM-MEMBER">/XYZ/dro</TEAM-MEMBER-REF>
                <DATE>2015-03-25</DATE>
            </DOC-REVISION>
        </DOC-REVISIONS>
    </ADMIN-DATA>





谢谢。



Thanks.

推荐答案

你可以首先看下面的文章



使用XPath和XmlDocument(C#)操纵XML数据 [ ^ ]
You can start by looking at below article

Manipulate XML data with XPath and XmlDocument (C#)[^]






我认为你想处理反序列化的对象,并希望在指定的路径上找到一个对象。下面的代码片段可能会帮助您完成此操作。



Hi,

I think you would like to deal with the deserialized object and want to find an object at specified path. Below code snippet might help you in doing this.

class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student { ID = 1, Name = "Abc", Department = new Department { ID = 1, Name = "Xyz" } };

            object result = student.Find("Department", "Name");

            Console.WriteLine(result);
            Console.ReadKey();
        }

        class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public Department Department { get; set; }
        }

        class Department
        {
            public int ID{ get; set; }
            public string Name { get; set; }
        }        
    }

    static class MiscExtensions
    {
        public static object Find(this object o, params string[] keys)
        {
            if (o == null) throw new ArgumentNullException();
            if (!keys.Any()) throw new ArgumentNullException();

            Type t = o.GetType();

            foreach (var property in t.GetProperties())
            {
                if (property.Name.Equals(keys[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    if (keys.Count() > 1)
                        return property.GetValue(o).Find(keys.Skip(1).ToArray());
                    else
                    {
                        return property.GetValue(o);
                    }
                }
            }

            throw new Exception("Path not found");
        }
    }


这篇关于Linq to XML或XPath从特定XML节点获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆