XLINQ:如何使用 XLINQ 排除 XML 中的节点
您好,我有以下 xml,
<students>
<student>
<id>12</id>
<name>Mohsan</name>
</student>
<student>
<id>2</id>
<name>Ali</name>
<address>
<country>Pakistan</country>
</address>
<address>
<country>India</country>
</address>
<parent>
<id>12</id>
<address>
<country>Pakistan</country>
</address>
</parent>
</student>
<student>
<id>3</id>
<name>Azhar</name>
</student>
</students>
我只想获取学生的地址。不是其父级的地址。我使用了这个查询,
var stds = from std in doc.Descendants("student")
select new
{
ID = std.Element("id").Value,
Name = std.Element("name").Value,
Address = from addr in std.Descendants("address")
select addr.Element("country").Value
};
但是这个查询也返回了它的父地址。我不想要。请注意,一个学生可以有多个地址。请告诉我如何在检索时排除父地址。
hi i have the following xml
<students>
<student>
<id>12</id>
<name>Mohsan</name>
</student>
<student>
<id>2</id>
<name>Ali</name>
<address>
<country>Pakistan</country>
</address>
<address>
<country>India</country>
</address>
<parent>
<id>12</id>
<address>
<country>Pakistan</country>
</address>
</parent>
</student>
<student>
<id>3</id>
<name>Azhar</name>
</student>
</students>
i want to get the address of student only. not the address of its parent. i used this query
var stds = from std in doc.Descendants("student")
select new
{
ID = std.Element("id").Value,
Name = std.Element("name").Value,
Address = from addr in std.Descendants("address")
select addr.Element("country").Value
};
but this query returning me address of its parent too. which i dont want. note that a student can have multiple address. please tell me how to exclude the parent address at the time of retrieval..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 std.Elements("address") 代替 std.Descendants("address")
Use
std.Elements("address")
instead ofstd.Descendants("address")