我想使用Id on <page>tag 读取此xml文件.
<?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>
Run Code Online (Sandbox Code Playgroud)
我如何通过id获取所有数据?我想使用LINQ to XML来完成这项任务.
鉴于您的XML已加载到XmlDocument变量' doc':
XmlNode page = doc.SelectSingleNode("//page[@id='OutStory']");
Run Code Online (Sandbox Code Playgroud)
即如果你想使用变量id:
XmlNode page = doc.SelectSingleNode("//page[@id='" + pageId + "']");
Run Code Online (Sandbox Code Playgroud)
这两个都可以让你做到:
string headerText = page.SelectSingleNode("./HeaderText").InnerText;
Run Code Online (Sandbox Code Playgroud)
编辑
如果你正在使用LINQ to XML,你的变量doc将是数据类型XDocument,你会写:
XElement page = doc.Descendants("page").Where(p => p.Attribute("id").Value == "OutStory").FirstOrDefault();
string headerText = page.Descendants("HeaderText").First().Value;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208 次 |
| 最近记录: |