简体   繁体   中英

How to read XML using Linq to XML?

My XML file does not have repeated info (eg Feed xml file). I just need some selected info from the xml file.

<?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? 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:

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:

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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