栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

获取XPath到XElement?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取XPath到XElement?

扩展方法:

public static class XExtensions{    /// <summary>    /// Get the absolute XPath to a given XElement    /// (e.g. "/people/person[6]/name[1]/last[1]").    /// </summary>    public static string GetAbsoluteXPath(this XElement element)    {        if (element == null)        { throw new ArgumentNullException("element");        }        Func<XElement, string> relativeXPath = e =>        { int index = e.IndexPosition(); string name = e.Name.LocalName; // If the element is the root, no index is required return (index == -1) ? "/" + name : string.Format (     "/{0}[{1}]",     name,      index.ToString() );        };        var ancestors = from e in element.Ancestors()  select relativeXPath(e);        return string.Concat(ancestors.Reverse().ToArray()) +     relativeXPath(element);    }    /// <summary>    /// Get the index of the given XElement relative to its    /// siblings with identical names. If the given element is    /// the root, -1 is returned.    /// </summary>    /// <param name="element">    /// The element to get the index of.    /// </param>    public static int IndexPosition(this XElement element)    {        if (element == null)        { throw new ArgumentNullException("element");        }        if (element.Parent == null)        { return -1;        }        int i = 1; // Indexes for nodes start at 1, not 0        foreach (var sibling in element.Parent.Elements(element.Name))        { if (sibling == element) {     return i; } i++;        }        throw new InvalidOperationException ("element has been removed from its parent.");    }}

和测试:

class Program{    static void Main(string[] args)    {        Program.Process(Xdocument.Load(@"C:test.xml").Root);        Console.Read();    }    static void Process(XElement element)    {        if (!element.HasElements)        { Console.WriteLine(element.GetAbsoluteXPath());        }        else        { foreach (XElement child in element.Elements()) {     Process(child); }        }    }}

并输出示例:

/tests/test[1]/date[1]/tests/test[1]/time[1]/start[1]/tests/test[1]/time[1]/end[1]/tests/test[1]/facility[1]/name[1]/tests/test[1]/facility[1]/website[1]/tests/test[1]/facility[1]/street[1]/tests/test[1]/facility[1]/state[1]/tests/test[1]/facility[1]/city[1]/tests/test[1]/facility[1]/zip[1]/tests/test[1]/facility[1]/phone[1]/tests/test[1]/info[1]/tests/test[2]/date[1]/tests/test[2]/time[1]/start[1]/tests/test[2]/time[1]/end[1]/tests/test[2]/facility[1]/name[1]/tests/test[2]/facility[1]/website[1]/tests/test[2]/facility[1]/street[1]/tests/test[2]/facility[1]/state[1]/tests/test[2]/facility[1]/city[1]/tests/test[2]/facility[1]/zip[1]/tests/test[2]/facility[1]/phone[1]/tests/test[2]/info[1]

那应该解决这个。没有?



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421464.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号