简体   繁体   English

如何使用Linq to XML读取XML?

[英]How to read XML using Linq to XML?

My XML file does not have repeated info (eg Feed xml file). 我的XML文件没有重复的信息(例如Feed xml文件)。 I just need some selected info from the xml file. 我只需要从xml文件中选择的一些信息。

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <client>
        <Name>abc, xyz's</Name>
        <DOB>2/1/1922</DOB>
        <Number>1234567896</Number>
        <Gender>unknown</Gender>
    </client>
    <Info>
        <ID>1111111111</ID>
        <Title>TITLE</Title>
    </Info>
    <BasicInfo>
        <TransDate>3/16/2011</TransDate>
        <Channel>1 + 1</Channel>
        <Ind></Ind>
        <Med></Med>
        <Comment>This is comment</Comment>
    </BasicInfo>
</root>

From this above file, I just need value of following elements:- 从上面的文件中,我只需要以下元素的值:

  • Name 名称
    • Number
    • Title 标题
    • Comment 评论

How to read this file using Linq to XML? 如何使用Linq to XML读取此文件? Please help. 请帮忙。

Simple: 简单:

XDocument doc = XDocument.Load("feed.xml");

XElement client = doc.Root.Element("client");
string name = (string) client.Element("Name");
int number = (int) client.Element("Number");

XElement info = doc.Root.Element("Info");
string title = (string) info.Element("Title");

XElement basicInfo = doc.Root.Element("BasicInfo");
string comment = (string) basicInfo.Element("Comment");

That could be made shorter, but having the separate variables for the different elements will make debugging easier. 可以将其缩短,但是为不同的元素使用单独的变量将使调试更加容易。 Of course, the above code has no error checking at all... depending on your situation, you may want loads or none :) 当然,以上代码根本没有错误检查...根据您的情况,您可能需要加载或不加载:)

Using XPath is a nice option here: 在这里使用XPath是一个不错的选择:

XDocument doc = XDocument.Load("file.xml");
string name = (string)doc.XPathSelectElement("//root/client/Name");
string title = (string)doc.XPathSelectElement("//root/Info/Title");
string comment = (string)doc.XPathSelectElement("//root/BasicInfo/Comment");

There's no error checking, but if you know the elements will be there this works well. 没有错误检查,但是如果您知道元素将存在,则效果很好。

Just in case you have been forced (like myself) to use VB.Net :), here is a possible solution: 万一您被迫(像我一样)被迫使用VB.Net :),这是一个可能的解决方案:

Dim xdoc As XDocument = <?xml version="1.0" encoding="UTF-8"?>
                                    <root>
                                        <client>
                                            <Name>abc, xyz's</Name>
                                            <DOB>2/1/1922</DOB>
                                            <Number>1234567896</Number>
                                            <Gender>unknown</Gender>
                                        </client>
                                        <Info>
                                            <ID>1111111111</ID>
                                            <Title>TITLE</Title>
                                        </Info>
                                        <BasicInfo>
                                            <TransDate>3/16/2011</TransDate>
                                            <Channel>1 + 1</Channel>
                                            <Ind></Ind>
                                            <Med></Med>
                                            <Comment>This is comment</Comment>
                                        </BasicInfo>
                                    </root>


            Console.WriteLine(xdoc.<root>.<client>.<Name>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<client>.<Number>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<Info>.<Title>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<BasicInfo>.<Comment>.Value())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM