User13839404
User13839404

Reputation: 1813

How to read XML using Linq to XML?

My XML file does not have repeated info (e.g. 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:-

How to read this file using Linq to XML? Please help.

Upvotes: 0

Views: 1172

Answers (3)

AlfieJ
AlfieJ

Reputation: 369

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.

Upvotes: 1

UncleZen
UncleZen

Reputation: 289

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499760

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

Upvotes: 3

Related Questions