[英]How to find path of root to node in xml with c#?
I want to load all of element in memory and find a list of root to node paths for them. 我想将所有元素加载到内存中,并找到它们的根到节点路径的列表。 for example in this XML :
例如在以下XML中:
<SigmodRecord>
<issue>
<volume>11</volume>
<number>1</number>
<articles>
<article>
<title>Annotated Bibliography on Data Design.</title>
<initPage>45</initPage>
<endPage>77</endPage>
<authors>
<author position="00">Anthony I. Wasserman</author>
<author position="01">Karen Botnich</author>
</authors>
</article>
<article>
<title>Architecture of Future Data Base Systems.</title>
<initPage>30</initPage>
<endPage>44</endPage>
<authors>
<author position="00">Lawrence A. Rowe</author>
<author position="01">Michael Stonebraker</author>
</authors>
</article>
<article>
<title>Database Directions III Workshop Review.</title>
<initPage>8</initPage>
<endPage>8</endPage>
<authors>
<author position="00">Tom Cook</author>
</authors>
</article>
<article>
<title>Errors in 'Process Synchronization in Database Systems'.</title>
<initPage>9</initPage>
<endPage>29</endPage>
<authors>
<author position="00">Philip A. Bernstein</author>
<author position="01">Marco A. Casanova</author>
<author position="02">Nathan Goodman</author>
</authors>
</article>
</articles>
</issue>
</SigmodRecord>
the answer must be something like this : 答案一定是这样的:
1 /SigmodRecord 1 / SigmodRecord
2 /SigmodRecord/issue 2 / SigmodRecord /问题
3 /SigmodRecord/issue/volume 3 / SigmodRecord /问题/卷
4 /SigmodRecord/issue/number 4 / SigmodRecord /问题/编号
5 /SigmodRecord/issue/articles 5 / SigmodRecord /问题/文章
6 /SigmodRecord/issue/articles/article 6 / SigmodRecord /问题/文章/文章
7 /SigmodRecord/issue/articles/article/title 7 / SigmodRecord /问题/文章/文章/标题
8 /SigmodRecord/issue/articles/article/authors 8 / SigmodRecord /问题/文章/文章/作者
9 /SigmodRecord/issue/articles/article/initPage 9 / SigmodRecord /问题/文章/文章/ initPage
10 /SigmodRecord/issue/articles/article/endPage 10 / SigmodRecord / issue / articles / article / endPage
11 /SigmodRecord/issue/articles/article/authors/author 11 / SigmodRecord /问题/文章/文章/作者/作者
You can use XLinq
to query the XML document and fetch root nodes and it`s descendants. 您可以使用
XLinq
查询XML文档,并获取根节点及其后代。
XDocument xDoc = XDocument.Load("myXml.xml");
XElement element = null;
if(xDoc!=null)
{
element=xDoc.Root;
}
var descendants=element.DescendantsAndSelf(); //Returns collection of descancdants
var descendants=element.DescendantsAndSelf("nodeName");//Filters to send only nodes with specified name.
Hope it helps!!! 希望能帮助到你!!!
One possible way, by recursively extracting path for each XML element * : 一种可能的方法是,递归地为每个XML元素*提取路径:
public static List<string> GetXpaths(XDocument doc)
{
var xpathList = new List<string>();
var xpath = "";
foreach(var child in doc.Elements())
{
GetXPaths(child, ref xpathList, xpath);
}
return xpathList;
}
public static void GetXPaths(XElement node, ref List<string> xpathList, string xpath)
{
xpath += "/" + node.Name.LocalName;
if (!xpathList.Contains(xpath))
xpathList.Add(xpath);
foreach(XElement child in node.Elements())
{
GetXPaths(child, ref xpathList, xpath);
}
}
Usage example in console application : 控制台应用程序中的用法示例:
var doc = XDocument.Load("path_to_your_file.xml");
var result = GetXpaths(doc);
foreach(var path in result)
Console.WriteLine(path);
.NET Fiddle demo .NET Fiddle演示
* ) Adapted from my old answer to another question . * )从我以前的答案改编为另一个问题 。 Note that this only worked for simple XML without namespace.
请注意,这仅适用于没有名称空间的简单XML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.