修改 LINQ to XML 语句以从嵌套 xml 中获取特定数据

发布于 2024-12-17 06:33:57 字数 1447 浏览 0 评论 0原文

我在之前关于如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-12-24 06:33:57

我认为你可以通过第一个查询中的另一个 linq 查询来解决这个问题。

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 = (from x in 
                       Person.Descendants("StuffToGetChild1") select new 
                        {
                            ...
                        })
                      };

I think you can resolve this problem by another linq query inside first one.

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 = (from x in 
                       Person.Descendants("StuffToGetChild1") select new 
                        {
                            ...
                        })
                      };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文