C# 基于XML中给定的子id获取根节点id
以下是我的XML结构:C# 基于XML中给定的子id获取根节点id,c#,asp.net,xml,C#,Asp.net,Xml,以下是我的XML结构: <DirectoryNode Id="297fe1ac-ff9b-4c40-ada4-86dc713a9537" Title="Root"> <DirectoryNode Id="80e01248-1170-4393-a327-b97409d51159" Title="A"> <DirectoryNode Id="e6db6d3f-be30-4cdf-b79e-864cc9b52c5f" Title="A1">
<DirectoryNode Id="297fe1ac-ff9b-4c40-ada4-86dc713a9537" Title="Root">
<DirectoryNode Id="80e01248-1170-4393-a327-b97409d51159" Title="A">
<DirectoryNode Id="e6db6d3f-be30-4cdf-b79e-864cc9b52c5f" Title="A1">
<DirectoryNode Id="4368d898-6fb0-4e2f-ba56-edbaf4bd0077" Title="A11" />
</DirectoryNode>
<DirectoryNode Id="11c5336b-462e-45dc-b4d3-92032ebc3ae3" Title="A2" />
</DirectoryNode>
<DirectoryNode Id="b983fd39-fc2e-43e0-80e6-3808fb47f995" Title="B">
<DirectoryNode Id="433851d6-9935-4adb-9acb-7055c26e85cb" Title="B1">
<DirectoryNode Id="f2602aed-6d97-4e46-9e8a-fb181b28f0c8" Title="B11" />
</DirectoryNode>
</DirectoryNode>
<DirectoryNode Id="9144d8cf-93c0-4de6-9109-448d396a9e17" Title="C" />
<DirectoryNode Id="182491af-452e-40bc-b51e-59f078db3ad3" Title="D" />
</DirectoryNode>
我试图做的是在给定目录节点id的情况下获取根节点(而不是主根节点)id。以下是一些示例:
当涉及到解析xml和提取值时,我很糟糕——有人知道一种快速的方法吗?听起来您可以使用以下方法:
XDocument doc = XDocument.Parse("nodes.xml");
XElement node = doc.Descendants("DirectoryNode")
.Where(n => (string) n.Attribute("Id") == id)
.Single();
// Now go up until we find the highest node beneatht the document root node
while (node.Parent != doc.Root)
{
node = node.Parent;
}
请注意,如果可以有多个具有相同ID的节点,或者如果该ID可能根本不存在,则必须更改查询。例如,如果您只关心第一个匹配,但可能没有匹配,则可以将Single
更改为FirstOrDefault()
,然后使用(在while
循环之前):
这里有一个小的递归方法
using System;
using System.Linq;
using System.Xml.Linq;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string sampleXml = @"<DirectoryNode Id=""297fe1ac-ff9b-4c40-ada4-86dc713a9537"" Title=""Root"">" + Environment.NewLine +
@" <DirectoryNode Id=""80e01248-1170-4393-a327-b97409d51159"" Title=""A"">" + Environment.NewLine +
@" <DirectoryNode Id=""e6db6d3f-be30-4cdf-b79e-864cc9b52c5f"" Title=""A1"">" + Environment.NewLine +
@" <DirectoryNode Id=""4368d898-6fb0-4e2f-ba56-edbaf4bd0077"" Title=""A11"" />" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" <DirectoryNode Id=""11c5336b-462e-45dc-b4d3-92032ebc3ae3"" Title=""A2"" />" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" <DirectoryNode Id=""b983fd39-fc2e-43e0-80e6-3808fb47f995"" Title=""B"">" + Environment.NewLine +
@" <DirectoryNode Id=""433851d6-9935-4adb-9acb-7055c26e85cb"" Title=""B1"">" + Environment.NewLine +
@" <DirectoryNode Id=""f2602aed-6d97-4e46-9e8a-fb181b28f0c8"" Title=""B11"" />" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" <DirectoryNode Id=""9144d8cf-93c0-4de6-9109-448d396a9e17"" Title=""C"" />" + Environment.NewLine +
@" <DirectoryNode Id=""182491af-452e-40bc-b51e-59f078db3ad3"" Title=""D"" />" + Environment.NewLine +
@"</DirectoryNode>" + Environment.NewLine +
@"";
XDocument document = XDocument.Parse(sampleXml);
// Given A11's Id, I want A's Id
XElement parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string) element.Attribute("Title") == "A11"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
// Given A2's Id, I want A's Id
parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string)element.Attribute("Title") == "A2"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
// Given A's Id, I want A's Id
parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string)element.Attribute("Title") == "A"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
// Given B1's Id, I want B's Id
parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string)element.Attribute("Title") == "B1"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
Console.ReadLine();
}
public static XElement GetParentDirectory(XElement element)
{
if (element.Parent == element.Document.Root)
return element;
return GetParentDirectory(element.Parent);
}
}
}
使用系统;
使用System.Linq;
使用System.Xml.Linq;
名称空间测试
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串sampleXml=@“+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@“”+Environment.NewLine+
@"";
XDocument document=XDocument.Parse(sampleXml);
//有了A11的身份证,我要A的身份证
XElement parentDirectory=GetParentDirectory(document.substands(“DirectoryNode”).Single(element=>(string)element.Attribute(“Title”)==“A11”);
Console.WriteLine(parentDirectory.Attribute(“Title”).Value);
//给定A2的Id,我要A的Id
parentDirectory=GetParentDirectory(document.subscriptions(“DirectoryNode”).Single(element=>(string)element.Attribute(“Title”)==“A2”);
Console.WriteLine(parentDirectory.Attribute(“Title”).Value);
//给A的身份证,我要A的身份证
parentDirectory=GetParentDirectory(document.subscriptions(“DirectoryNode”).Single(element=>(string)element.Attribute(“Title”)==“A”);
Console.WriteLine(parentDirectory.Attribute(“Title”).Value);
//给定B1的Id,我要B的Id
parentDirectory=GetParentDirectory(document.subscriptions(“DirectoryNode”).Single(element=>(string)element.Attribute(“Title”)==“B1”);
Console.WriteLine(parentDirectory.Attribute(“Title”).Value);
Console.ReadLine();
}
公共静态XElement GetParentDirectory(XElement元素)
{
if(element.Parent==element.Document.Root)
返回元素;
返回GetParentDirectory(element.Parent);
}
}
}
using System;
using System.Linq;
using System.Xml.Linq;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string sampleXml = @"<DirectoryNode Id=""297fe1ac-ff9b-4c40-ada4-86dc713a9537"" Title=""Root"">" + Environment.NewLine +
@" <DirectoryNode Id=""80e01248-1170-4393-a327-b97409d51159"" Title=""A"">" + Environment.NewLine +
@" <DirectoryNode Id=""e6db6d3f-be30-4cdf-b79e-864cc9b52c5f"" Title=""A1"">" + Environment.NewLine +
@" <DirectoryNode Id=""4368d898-6fb0-4e2f-ba56-edbaf4bd0077"" Title=""A11"" />" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" <DirectoryNode Id=""11c5336b-462e-45dc-b4d3-92032ebc3ae3"" Title=""A2"" />" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" <DirectoryNode Id=""b983fd39-fc2e-43e0-80e6-3808fb47f995"" Title=""B"">" + Environment.NewLine +
@" <DirectoryNode Id=""433851d6-9935-4adb-9acb-7055c26e85cb"" Title=""B1"">" + Environment.NewLine +
@" <DirectoryNode Id=""f2602aed-6d97-4e46-9e8a-fb181b28f0c8"" Title=""B11"" />" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" </DirectoryNode>" + Environment.NewLine +
@" <DirectoryNode Id=""9144d8cf-93c0-4de6-9109-448d396a9e17"" Title=""C"" />" + Environment.NewLine +
@" <DirectoryNode Id=""182491af-452e-40bc-b51e-59f078db3ad3"" Title=""D"" />" + Environment.NewLine +
@"</DirectoryNode>" + Environment.NewLine +
@"";
XDocument document = XDocument.Parse(sampleXml);
// Given A11's Id, I want A's Id
XElement parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string) element.Attribute("Title") == "A11"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
// Given A2's Id, I want A's Id
parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string)element.Attribute("Title") == "A2"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
// Given A's Id, I want A's Id
parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string)element.Attribute("Title") == "A"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
// Given B1's Id, I want B's Id
parentDirectory = GetParentDirectory(document.Descendants("DirectoryNode").Single(element => (string)element.Attribute("Title") == "B1"));
Console.WriteLine(parentDirectory.Attribute("Title").Value);
Console.ReadLine();
}
public static XElement GetParentDirectory(XElement element)
{
if (element.Parent == element.Document.Root)
return element;
return GetParentDirectory(element.Parent);
}
}
}