Linq to xml:找出父级?

发布于 2025-01-08 10:32:52 字数 2079 浏览 0 评论 0原文

我正在尝试使用 XElement 找出属性 Id 的父节点。请参阅下面的示例 这是我的数据..

<Data>
      <Description>MASTER</Description>
      <Data>
        <Description>Parent1</Description>
        <Data id="GUID1" Description="THIS IS A TEST" />
        <Data id="GUID2" Description="THIS IS A TEST" />
        <Data id="GUID3" Description="THIS IS A TEST" />
      </Data>
      <Data>
        <Description>Parent2</Description>
        <Data id="GUID4" Description="THIS IS A TEST" />
        <Data id="GUID5" Description="THIS IS A TEST" />
      </Data>
      <Data id="GUID6" Description="THIS IS A TEST" />
    </Data>

如果我想找出“GUID6”和“GUID1”父母,结果将是 GUID6 =“MASTER” 对于 GUID1="MASTERParent1" 但我的结果不正确。请有人帮助我。

我还希望输出为“MASTER.Parent1”,因此它通过使用“点”将主节点和父节点分开,

这是我的代码

protected void Page_Load(object sender, EventArgs e)
        {
            var s = "<Data><Description>MASTER</Description><Data><Description>PARENT1</Description><Data id=\"GUID1\" Description=\"THIS IS A TEST\" /><Data id=\"GUID2\" Description=\"THIS IS A TEST\" /><Data id=\"GUID3\" Description=\"THIS IS A TEST\" /></Data><Data><Description>PARENT2</Description><Data id=\"GUID4\" Description=\"THIS IS A TEST\" /><Data id=\"GUID5\" Description=\"THIS IS A TEST\" /></Data><Data id=\"GUID6\" Description=\"THIS IS A TEST\" /></Data>";
            var doc = XElement.Load(new StringReader(s));
            var result = (from data in doc.Descendants("Data").Where(x => x.Attribute("id") != null)
                          select new
                              {
                                  Id = data.Attribute("id").Value,
                                  Decription = data.Attribute("Description").Value,
                                  Parent = data.Parent.Value
                              }).ToList();


        }

I'm trying to use XElement to find out the parent node of a attribute Id. See example below
This is my data..

<Data>
      <Description>MASTER</Description>
      <Data>
        <Description>Parent1</Description>
        <Data id="GUID1" Description="THIS IS A TEST" />
        <Data id="GUID2" Description="THIS IS A TEST" />
        <Data id="GUID3" Description="THIS IS A TEST" />
      </Data>
      <Data>
        <Description>Parent2</Description>
        <Data id="GUID4" Description="THIS IS A TEST" />
        <Data id="GUID5" Description="THIS IS A TEST" />
      </Data>
      <Data id="GUID6" Description="THIS IS A TEST" />
    </Data>

If I want to find out "GUID6" and "GUID1" parents the result would be for GUID6 = "MASTER"
and for GUID1="MASTERParent1" but I my results are incorrect. Please can someone help me.

I also want the output to be "MASTER.Parent1" so it splits up the master and parent by using a "dot"

Here is my code

protected void Page_Load(object sender, EventArgs e)
        {
            var s = "<Data><Description>MASTER</Description><Data><Description>PARENT1</Description><Data id=\"GUID1\" Description=\"THIS IS A TEST\" /><Data id=\"GUID2\" Description=\"THIS IS A TEST\" /><Data id=\"GUID3\" Description=\"THIS IS A TEST\" /></Data><Data><Description>PARENT2</Description><Data id=\"GUID4\" Description=\"THIS IS A TEST\" /><Data id=\"GUID5\" Description=\"THIS IS A TEST\" /></Data><Data id=\"GUID6\" Description=\"THIS IS A TEST\" /></Data>";
            var doc = XElement.Load(new StringReader(s));
            var result = (from data in doc.Descendants("Data").Where(x => x.Attribute("id") != null)
                          select new
                              {
                                  Id = data.Attribute("id").Value,
                                  Decription = data.Attribute("Description").Value,
                                  Parent = data.Parent.Value
                              }).ToList();


        }

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

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

发布评论

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

评论(1

感受沵的脚步 2025-01-15 10:32:52

我觉得你的例子和你的要求不相符。看看我的解释是否是您想要的。

var s = @"
<Data>
    <Description>MASTER</Description>
    <Data>
        <Description>Parent1</Description>
        <Data id=""GUID1"" Description=""THIS IS A TEST"" />
        <Data id=""GUID2"" Description=""THIS IS A TEST"" />
        <Data id=""GUID3"" Description=""THIS IS A TEST"" />
    </Data>
    <Data>
        <Description>Parent2</Description>
        <Data id=""GUID4"" Description=""THIS IS A TEST"" />
        <Data id=""GUID5"" Description=""THIS IS A TEST"" />
    </Data>
    <Data id=""GUID6"" Description=""THIS IS A TEST"" />
</Data>";

var doc = XElement.Load(new StringReader(s));

var desiredGuids = new List<string>{ "GUID1", "GUID6" };

var result = (from data in doc.Descendants("Data")
            where data.Attribute("id") != null && desiredGuids.Contains((string)data.Attribute("id"))
            select new { 
                Id = data.Parent.Element("Description").Value + "." + (string)data.Attribute("id"),
                Description = (string)data.Attribute("Description"),
                Parent = data.Parent.Element("Description").Value
            });

foreach (var element in result)
{
    Console.WriteLine("{0} {1}", element.Id, element.Description);
}

结果是这个输出

Parent1.GUID1 THIS IS A TEST
MASTER.GUID6 THIS IS A TEST

I feel like your example and what you are asking for don't match up. See if my interpretation is what you are looking for.

var s = @"
<Data>
    <Description>MASTER</Description>
    <Data>
        <Description>Parent1</Description>
        <Data id=""GUID1"" Description=""THIS IS A TEST"" />
        <Data id=""GUID2"" Description=""THIS IS A TEST"" />
        <Data id=""GUID3"" Description=""THIS IS A TEST"" />
    </Data>
    <Data>
        <Description>Parent2</Description>
        <Data id=""GUID4"" Description=""THIS IS A TEST"" />
        <Data id=""GUID5"" Description=""THIS IS A TEST"" />
    </Data>
    <Data id=""GUID6"" Description=""THIS IS A TEST"" />
</Data>";

var doc = XElement.Load(new StringReader(s));

var desiredGuids = new List<string>{ "GUID1", "GUID6" };

var result = (from data in doc.Descendants("Data")
            where data.Attribute("id") != null && desiredGuids.Contains((string)data.Attribute("id"))
            select new { 
                Id = data.Parent.Element("Description").Value + "." + (string)data.Attribute("id"),
                Description = (string)data.Attribute("Description"),
                Parent = data.Parent.Element("Description").Value
            });

foreach (var element in result)
{
    Console.WriteLine("{0} {1}", element.Id, element.Description);
}

results in this output

Parent1.GUID1 THIS IS A TEST
MASTER.GUID6 THIS IS A TEST
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文