1

I want to read this xml file using Id on <page> tag.

<?xml version="1.0" encoding="utf-8" ?>
    <pages>
      <page id="NewsWatchVideo">
        <HeaderText>Newswatch</HeaderText>
        <BellowText>'abc'.In this video you will see how the press responded to the   .</BellowText>
        <FilePath>FLVPlayer_Progressive.swf</FilePath>
        <NextURL>/Home/OutStory</NextURL>
      </page>
      <page id="OutStory">
        <HeaderText>OUR STORY</HeaderText>
        <BellowText>Join the founders of United First Financial as they talk about how the business and concept was formed.</BellowText>
        <FilePath>FLVPlayer_Progressive.swf</FilePath>
        <NextURL>/Home/MMaoverViewVideo</NextURL>
      </page>
    </pages>

How can i get all the data by id? I want to use LINQ to XML to do this task.

2
  • 1
    I don't understand what you mean by "parse data by id"? Do you want to get a list of id's? Do you want to get all the data for one Id? Do you want to traverse id's in order? Commented Jan 4, 2010 at 11:18
  • i assumed 'get all the data for one id', but yeah, that's a very valid question Commented Jan 4, 2010 at 11:20

1 Answer 1

6

Given that your XML is loaded into XmlDocument variable 'doc':

XmlNode page = doc.SelectSingleNode("//page[@id='OutStory']");

i.e. if you want to use a variable id:

XmlNode page = doc.SelectSingleNode("//page[@id='" + pageId + "']");

Both of which will allow you to do:

string headerText = page.SelectSingleNode("./HeaderText").InnerText;

EDIT

If you're working with LINQ to XML, your variable doc will be of the datatype XDocument and you'll write:

XElement page = doc.Descendants("page").Where(p => p.Attribute("id").Value == "OutStory").FirstOrDefault();
string headerText = page.Descendants("HeaderText").First().Value;
Sign up to request clarification or add additional context in comments.

1 Comment

Dear @@David how can i do this task using LINQ to XML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.