Linq to xml:找出父级?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我觉得你的例子和你的要求不相符。看看我的解释是否是您想要的。
结果是这个输出
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.
results in this output