简体   繁体   中英

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 :

<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
2 /SigmodRecord/issue
3 /SigmodRecord/issue/volume
4 /SigmodRecord/issue/number
5 /SigmodRecord/issue/articles
6 /SigmodRecord/issue/articles/article
7 /SigmodRecord/issue/articles/article/title
8 /SigmodRecord/issue/articles/article/authors
9 /SigmodRecord/issue/articles/article/initPage
10 /SigmodRecord/issue/articles/article/endPage
11 /SigmodRecord/issue/articles/article/authors/author

You can use XLinq to query the XML document and fetch root nodes and it`s descendants.

    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 * :

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

* ) Adapted from my old answer to another question . Note that this only worked for simple XML without namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM