开发者

parse nodes using c#

I want to read this xml file using Id on 开发者_如何学C<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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜