using namespace in xpath for SelectSingleNode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angela

    using namespace in xpath for SelectSingleNode

    I am getting an error when I attempt to access a node with
    SelectSingleNod e(): "The expression passed to this method should result in a
    NodeSet." I understand there is some confusion when you have to state a
    namespace in the xpath. Can the node be accessible by the xpath specified?
    Thanks.



    XmlDocument xmldoc = new XmlDocument();

    XmlCDataSection cdata = xmldoc.CreateCD ataSection(requ estString);



    String soapEnvelope =

    "<SOAP-ENV:Envelope
    xmlns:SOAP-ENV=\"http://schemas.xmlsoap .org/soap/envelope/\"

    xmlns:xsi=\"htt p://wwww3.org/1999/XMLSchema-instance\"

    xmlns:xsd\"http ://www.w3.org/1999/XMLSchema\">

    <SOAP-ENV:Body>

    <m:submit xmlns:m=\"com.a bc.def\">

    <parameter1>TES T</parameter1>

    <parameter2></parameter2>

    </m:submit>

    </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>";

    xmldoc.LoadXml( soapEnvelope);



    XmlNode xmlnode =
    xmldoc.Document Element.SelectS ingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/m:submit/parameter2/");

    xmlnode.AppendC hild(cdata);


  • Martin Honnen

    #2
    Re: using namespace in xpath for SelectSingleNod e



    Angela wrote:
    [color=blue]
    > I am getting an error when I attempt to access a node with
    > SelectSingleNod e(): "The expression passed to this method should result in a
    > NodeSet." I understand there is some confusion when you have to state a
    > namespace in the xpath. Can the node be accessible by the xpath specified?[/color]
    [color=blue]
    > XmlDocument xmldoc = new XmlDocument();
    >
    > XmlCDataSection cdata = xmldoc.CreateCD ataSection(requ estString);
    >
    >
    >
    > String soapEnvelope =
    >
    > "<SOAP-ENV:Envelope
    > xmlns:SOAP-ENV=\"http://schemas.xmlsoap .org/soap/envelope/\"
    >
    > xmlns:xsi=\"htt p://wwww3.org/1999/XMLSchema-instance\"
    >
    > xmlns:xsd\"http ://www.w3.org/1999/XMLSchema\">
    >
    > <SOAP-ENV:Body>
    >
    > <m:submit xmlns:m=\"com.a bc.def\">
    >
    > <parameter1>TES T</parameter1>
    >
    > <parameter2></parameter2>
    >
    > </m:submit>
    >
    > </SOAP-ENV:Body>
    >
    > </SOAP-ENV:Envelope>";
    >
    > xmldoc.LoadXml( soapEnvelope);
    >[/color]

    You should create and use a namespace manager e.g.
    XmlNamespaceMan ager namespaceManage r = new
    XmlNamespaceMan ager(xmldoc.Nam eTable);
    then add the namespaces you need e.g.
    namespaceManage r.Add("SOAP-ENV",
    "http://schemas.xmlsoap .org/soap/envelope");
    namespaceManage r.Add("m", "com.abc.de f");
    then pass the namespace manager to the SelectSingleNod e method[color=blue]
    > XmlNode xmlnode =
    > xmldoc.Document Element.SelectS ingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/m:submit/parameter2/");[/color]

    xmldoc.Document Element.SelectS ingleNode(xpath Expression,
    namespaceManage r)

    --

    Martin Honnen --- MVP XML

    Comment

    Working...