• .NET XmlNavigator with Namespace


    In .NET 2.0 there is additional overload that accepts string expression
    and IXmlNamespaceResolver (XmlNamespaceManager implements it) - see
    http://msdn2.microsoft.com/en-us/library/6k4x060d.aspx.

    XPathDocument document = new XPathDocument("contosoBooks.xml");
    XPathNavigator navigator 
    = document.CreateNavigator();

    XmlNamespaceManager manager 
    = new XmlNamespaceManager(navigator.NameTable);
    manager.AddNamespace(
    "bk""http://www.contoso.com/books");

    XPathNodeIterator nodes 
    = navigator.Select("/bk:bookstore/bk:book", manager);
    XPathNavigator nodesNavigator 
    = nodes.Current;

    XPathNodeIterator nodesText 
    = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

    while (nodesText.MoveNext())
    {
        Console.Write(nodesText.Current.Name);
        Console.WriteLine(nodesText.Current.Value);
    }


    In .NET 1.1 you have no choice but to compile XPathExpression manually:

    XPathExpression expr = nav.Compile("book/@bk:ISBN");
    XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
    mngr.AddNamespace("bk","urn:samples");
    expr.SetContext(mngr);
    XPathNodeIterator ni = nav.Select(expr);

    See
    http://msdn.microsoft.com/library/d...ecttopic2 .asp

    --------------------------------------------------------------------------------


    Thanks Oleg, it works a treat (but what a polaver though).

    One small follow-up if I may. I would have thought that if I initialized the
    namespace manager like this:

    XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);

    then it would pick up the namespace prefixes from the document. However, it
    doesn't (I still need to do the AddNamespace calls)

     --------------------------------------------------------------------------------

    No, first of all NameTable has nothing to do with namespaces - it's just
    a collection of atomic strings (names) used to save memory and speed
    up name comparisons. Second - there is no such thing as "prefixes from
    the document" as in XML document namespace prefixes can be overriden and
    undeclared in any order.

    The NameTable is used to speed up string comparison, there is nothing
    done like "picking up namespace prefixes".
    As said, with .NET 2.0 you have some way to use the XPathNavigator
    itself, once moved to the proper node you want to "pick up namespace
    prefixes" from, itself as the namespace manager.
    <http://groups.google.com/group/microsoft.public.dotnet.xml/msg/af7d7f9e2f422d7e?hl=en&>

  • 相关阅读:
    Linq to sql(四):查询句法(一)
    (转帖)用戶控件包裝器的设计与实现
    iptables 安装设置 针对游戏服务器
    Shell脚本加密
    Nginx配置文件nginx.conf中文详解
    C#获取默认浏览器的完整地址并在默认浏览器中打开对应的url地址
    HttpWebRequest类
    解决问题记录(3)-事务处理出错:连接打开但是在fetching状态中。
    程序生涯的几个感悟
    解决问题记录(4)-Oracle Not Support Parallel Transaction
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/1140591.html
Copyright © 2020-2023  润新知