读取XML文件只有部分 [英] Read only parts of an XML file
本文介绍了读取XML文件只有部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<Peoples>
<People>
<Name>RadheyJang</Name>
<Location>India</Location>
<Work>Software Developer</Work>
<Point>5</Point>
<details>
<People>
<Name>ArunaTiwari</Name>
<Location>India</Location>
<Work>SoFtwareCoder</Work>
<Point>3</Point>
<details/>
<Test>A</Test>
</People>
</details>
<Test>NA</Test>
</People>
</Peoples>
我能够读取的Xml使用以下code。
I am able to Read That Xml By using below code .
XDocument xmlDoc = XDocument.Load(str);
var vrresult = from a in xmlDoc.Descendants("People")
select new
{
Name= a.Element("Name").Value,
Location= a.Element("Location").Value,
Point= a.Element("Point").Value
};
GridView1.DataSource = vrresult;
GridView1.DataBind();
但它是阅读的细节内容也。我想跳过读取里面的内容在详情元素。请让我知道我可以跳过里面的内容细节。
But It is reading the Contents of details also . I want to Skip reading the Content inside the details Element . Please let me know how can i skip the Content Inside the details .
推荐答案
您需要使用XPath这个...
You need to use XPath for this...
using System.Xml.XPath;
string xml = @"
<Peoples>
<People>
<Name>RadheyJang</Name>
<Location>India</Location>
<Work>Software Developer</Work>
<Point>5</Point>
<details>
<People>
<Name>ArunaTiwari</Name>
<Location>India</Location>
<Work>SoFtwareCoder</Work>
<Point>3</Point>
<details/>
<Test>A</Test>
</People>
</details>
<Test>NA</Test>
</People>
</Peoples>";
XDocument xmlDoc = XDocument.Parse(xml);
var vrresult = from a in xmlDoc.XPathSelectElements("/Peoples/People")
select new
{
Name = a.Element("Name").Value,
Location = a.Element("Location").Value,
Point = a.Element("Point").Value
};
这篇关于读取XML文件只有部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文