Linq to XML 嵌套标签加入问题

发布于 2024-10-21 04:10:19 字数 631 浏览 4 评论 0原文

我有一个如下所示的 XML,

<Labs>
  <Lab id="a" name="a">
    <Test name="aa"></Test>
    <Test name="ab"></Test>
    <Test name="ac"></Test>
  </Lab>
  <Lab id="b" name="b">
    <Test name="ba"></Test>
    <Test name="bb"></Test>
  </Lab>
</Labs>

我想通过单个 select 语句使用 LINQ to XML 从单个表中的 XML 中获取数据。如果您能给我提供如何进行查询的参考。

我将 Linq to XML 查询输出绑定到 silverlight 网格。在 silverlight 中输出应该是这样的......

LabName    TestName
a          aa
a          ab
a          ac
b          ba
b          bb

I have an XML that looks like this

<Labs>
  <Lab id="a" name="a">
    <Test name="aa"></Test>
    <Test name="ab"></Test>
    <Test name="ac"></Test>
  </Lab>
  <Lab id="b" name="b">
    <Test name="ba"></Test>
    <Test name="bb"></Test>
  </Lab>
</Labs>

I want to get the data from this XML in a single table using LINQ to XML through a single select statement. If you could provide me with a reference of how to do the query.

I am binding the Linq to XML query output to silverlight grid. The output should be like this in silverlight...

LabName    TestName
a          aa
a          ab
a          ac
b          ba
b          bb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

桃酥萝莉 2024-10-28 04:10:19

好的,这是一个工作示例:

string xml =@"<Labs>
  <Lab id='a'>
    <Test name='aa'></Test>
    <Test name='ab'></Test>
    <Test name='ac'></Test>
  </Lab>
  <Lab id='b'>
    <Test name='ba'></Test>
    <Test name='bb'></Test>
  </Lab>
</Labs>";

XDocument document = XDocument.Parse(xml);
IEnumerable<XElement> xElements = document.Descendants().Where(e => e.Name == "Test");
var results =  xElements.Select(m => new
                                        {
                                            Test = m.Attributes("name").FirstOrDefault().Value, 
                                                    Lab =m.Parent.Attributes("id").FirstOrDefault().Value
                                        });
        foreach (var result in results)
{
            Console.Write(result.Lab);
            Console.Write('\t');
            Console.WriteLine(result.Test);
}

OK here is a working example:

string xml =@"<Labs>
  <Lab id='a'>
    <Test name='aa'></Test>
    <Test name='ab'></Test>
    <Test name='ac'></Test>
  </Lab>
  <Lab id='b'>
    <Test name='ba'></Test>
    <Test name='bb'></Test>
  </Lab>
</Labs>";

XDocument document = XDocument.Parse(xml);
IEnumerable<XElement> xElements = document.Descendants().Where(e => e.Name == "Test");
var results =  xElements.Select(m => new
                                        {
                                            Test = m.Attributes("name").FirstOrDefault().Value, 
                                                    Lab =m.Parent.Attributes("id").FirstOrDefault().Value
                                        });
        foreach (var result in results)
{
            Console.Write(result.Lab);
            Console.Write('\t');
            Console.WriteLine(result.Test);
}
浮萍、无处依 2024-10-28 04:10:19

对于 Aliostad 发布的上述解决方案的 lambda 免费版本。你也可以用这个...

        var Tests = from tests in doc.Descendants("Test")
                    where tests.Attributes().Count() > 0
                    select new LabTestModel
                                {
                                    LabName = tests.Parent.Attribute("Name").Value,
                                    TestName = tests.Attribute("Name").Value
                                };

for a lambda free version of the above solution posted by Aliostad. You can use this as well...

        var Tests = from tests in doc.Descendants("Test")
                    where tests.Attributes().Count() > 0
                    select new LabTestModel
                                {
                                    LabName = tests.Parent.Attribute("Name").Value,
                                    TestName = tests.Attribute("Name").Value
                                };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文