修改 LINQ to XML 语句以从嵌套 xml 中获取特定数据
我在之前关于如何使用 Linq to XML 的问题中得到了帮助,我几乎让它工作了,但我坚持让嵌套正常工作。请记住,这是我第一次使用 LINQ,所以我对它完全是新手。我有一个 xml 文件,它作为流进入 Web 应用程序。 xml 非常简单,基本上遵循以下格式:
<root>
<people>
<person>
<StuffToGet1>
<StuffToGet2>
...
<StuffToGetPARENT>
<useless1>
<useless2>
...
<StuffToGetChild1>
<StuffToGetChild2>
</StuffToGetPARENT>
</person>
</people>
</root>
显然,我对于获取顶层的“要获取的内容”没有任何问题。目前,我的 Linq 正在撤回 .我想过滤子查询上的选择以忽略 StuffToGetChild 标记中不存在的所有内容。这些标签都将被命名为相同的东西(不是 Brand1、Brand2 等)。以下是我到目前为止所拥有的...请帮助!
XDocument xdoc = XDocument.Load(XmlReader.Create(responseStream));
var People = from Person in xdoc.Descendants("Person")
where Person.Element("Role").Value != "Admin"
orderby Specialist.Element("Role").Value
select new
{
StuffToGet1 = Person.Element("StuffToGet1").Value,
StuffToGet2 = Person.Element("StuffToGet2").Value,
Brand = Person.Element("StuffToGetPARENT").Value
};
gvTest.DataSource = People;
gvTest.DataBind();
编辑:我刚刚意识到我说的话可能有点令人困惑。 “品牌”实际上是“StuffToGetChild”标签中的内容。抱歉造成混乱。
I was helped out in an earlier question on how to use Linq to XML, and I almost have it working, but I'm stuck on getting the nesting working properly. Please keep in mind, this is the first time I've ever used LINQ, so I'm a complete newbie at it. I have an xml file, which comes into the web app as a stream. The xml is pretty straightforward, basically following the following format:
<root>
<people>
<person>
<StuffToGet1>
<StuffToGet2>
...
<StuffToGetPARENT>
<useless1>
<useless2>
...
<StuffToGetChild1>
<StuffToGetChild2>
</StuffToGetPARENT>
</person>
</people>
</root>
Obviously, I have no problem with getting the Stuff To Get's that are on the top level. Currently, my Linq is pulling back EVERYTHING in . I want to filter that select on a subquery to ignore everything that isnt in StuffToGetChild tags. The tags will all be named the same thing, (NOT Brand1, Brand2, etc). Below is what I have so far... please help!!
XDocument xdoc = XDocument.Load(XmlReader.Create(responseStream));
var People = from Person in xdoc.Descendants("Person")
where Person.Element("Role").Value != "Admin"
orderby Specialist.Element("Role").Value
select new
{
StuffToGet1 = Person.Element("StuffToGet1").Value,
StuffToGet2 = Person.Element("StuffToGet2").Value,
Brand = Person.Element("StuffToGetPARENT").Value
};
gvTest.DataSource = People;
gvTest.DataBind();
EDIT: I just realized something I said which might be a bit confusing. "Brand" is actually what's in the "StuffToGetChild" tags. Sorry for confusion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以通过第一个查询中的另一个 linq 查询来解决这个问题。
I think you can resolve this problem by another linq query inside first one.