Using Xpath With Default Namespace in C#

前端 未结 13 1511
别那么骄傲
别那么骄傲 2020-11-22 03:02

I\'ve got an XML document with a default namespace. I\'m using a XPathNavigator to select a set of nodes using Xpath as follows:

XmlElement myXML = ...;           


        
13条回答
  •  时光说笑
    2020-11-22 03:56

    In case the namespaces differ for outerelement and innerelement

    XmlNamespaceManager manager = new XmlNamespaceManager(myXmlDocument.NameTable);
                                manager.AddNamespace("o", "namespaceforOuterElement");
                                manager.AddNamespace("i", "namespaceforInnerElement");
    string xpath = @"/o:outerelement/i:innerelement"
    // For single node value selection
    XPathExpression xPathExpression = navigator.Compile(xpath );
    string reportID = myXmlDocument.SelectSingleNode(xPathExpression.Expression, manager).InnerText;
    
    // For multiple node selection
    XmlNodeList myNodeList= myXmlDocument.SelectNodes(xpath, manager);
    

提交回复
热议问题