XML parse

Discussion in 'ASP .Net' started by Stan SR, May 21, 2007.

  1. Stan SR

    Stan SR Guest

    Hi,

    I have an xml file with these nodes
    <soap:Envelope xmlns:soap="http://blabla">
    <soap:body>
    <ProcessResponse xmlns="http://anotherblabla">
    <myResult>
    <data _1>1</dada_1>
    <data_2>2</data_2>
    </myResult>
    </ProcessResponse>
    </soap:Body>
    <soap:Envelope>


    I need to perform a selectNode on myResult
    Like
    XmlNodeList
    nodes=doc.SelectNodes("/Envelope/body/ProcessResponse/myResult");

    But I m blocked with the different namespaces (blabla and anotherblabla).
    How can get my nodes selection when there are namespaces on some nodes ?
    Thanks

    Stan
     
    Stan SR, May 21, 2007
    #1
    1. Advertising

  2. Stan SR wrote:

    > <soap:Envelope xmlns:soap="http://blabla">
    > <soap:body>
    > <ProcessResponse xmlns="http://anotherblabla">
    > <myResult>
    > <data _1>1</dada_1>
    > <data_2>2</data_2>
    > </myResult>
    > </ProcessResponse>
    > </soap:Body>
    > <soap:Envelope>
    >
    >
    > I need to perform a selectNode on myResult
    > Like
    > XmlNodeList
    > nodes=doc.SelectNodes("/Envelope/body/ProcessResponse/myResult");
    >
    > But I m blocked with the different namespaces (blabla and anotherblabla).
    > How can get my nodes selection when there are namespaces on some nodes ?


    Use an XmlNamespaceManager and AddNamespace, assuming you have this
    snippet of XML:

    <soap:Envelope xmlns:soap="http://blabla">
    <soap:Body>
    <ProcessResponse xmlns="http://anotherblabla">
    <myResult>
    <data_1>1</data_1>
    <data_2>2</data_2>
    </myResult>
    </ProcessResponse>
    </soap:Body>
    </soap:Envelope>

    then you can use e.g.

    XmlNamespaceManager namespaceManager = new
    XmlNamespaceManager(xmlDocument.NameTable);
    namespaceManager.AddNamespace("soap", "http://blabla");
    namespaceManager.AddNamespace("pr", "http://anotherblabla");
    XmlNodeList nodes =
    xmlDocument.SelectNodes("soap:Envelope/soap:Body/pr:processResponse/pr:myResult",
    namespaceManager);


    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 21, 2007
    #2
    1. Advertising

  3. Stan SR

    Stan SR Guest

    Thanks Martin,

    I ve tried your code but it still doesn't work for me ...

    Here's the correct header of my xml file
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    soap:encodingStyle="http//schemas.xmlsoap.org/soap/encoding/">
    <soap:Body>
    <ProcessResponse xmlns="http://www.csc.com/graphtalk/">
    <POGEST>
    <ReturnStatus>
    <BUSINESS_STATUS>WM_SUCCESS</BUSINESS_STATUS>
    </ReturnStatus>
    <Data>
    ...
    </ProcessResponse>
    </soap:Body>
    </soap:Envelope>

    So here's my code

    XmlNamespaceManager nsmg = new XmlNamespaceManager(doc.NameTable);
    nsmg.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope");
    nsmg.AddNamespace("pr", "http://www.csc.com/graphtalk/");
    XmlNodeList nodestatut =
    doc.SelectNodes("soap:Envelope/soap:Body/pr:processResponse", nsmg);

    in fact, I need to get the this result

    XmlNodeList nodestatut =
    doc.SelectNodes("soap:Envelope/soap:Body/pr:processResponse/pr:pOGEST/pr:ReturnStatus",
    nsmg);


    Where 's the problem ? :-(
    Stan

    "Martin Honnen"

    > Use an XmlNamespaceManager and AddNamespace, assuming you have this
    > snippet of XML:
    >
    > <soap:Envelope xmlns:soap="http://blabla">
    > <soap:Body>
    > <ProcessResponse xmlns="http://anotherblabla">
    > <myResult>
    > <data_1>1</data_1>
    > <data_2>2</data_2>
    > </myResult>
    > </ProcessResponse>
    > </soap:Body>
    > </soap:Envelope>
    >
    > then you can use e.g.
    >
    > XmlNamespaceManager namespaceManager = new
    > XmlNamespaceManager(xmlDocument.NameTable);
    > namespaceManager.AddNamespace("soap", "http://blabla");
    > namespaceManager.AddNamespace("pr", "http://anotherblabla");
    > XmlNodeList nodes =
    > xmlDocument.SelectNodes("soap:Envelope/soap:Body/pr:processResponse/pr:myResult",
    > namespaceManager);
    >
    >
    > --
     
    Stan SR, May 21, 2007
    #3
  4. Stan SR wrote:

    > I ve tried your code but it still doesn't work for me ...
    >
    > Here's the correct header of my xml file
    > <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Compare that URL to this

    > nsmg.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope");

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    URL and you will see that the trailing slash is missing, that way you
    can't find the element.




    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 21, 2007
    #4
  5. Stan SR

    Stan SR Guest

    "Martin Honnen"

    >> Here's the correct header of my xml file
    >> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > Compare that URL to this
    >
    >> nsmg.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope");

    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > URL and you will see that the trailing slash is missing, that way you
    > can't find the element.



    Yes, I ve found the issue (and you were right)...
    Many thanks.

    Stan
     
    Stan SR, May 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. neil
    Replies:
    0
    Views:
    585
  2. Replies:
    1
    Views:
    746
    Esmond Pitt
    Mar 27, 2005
  3. Replies:
    19
    Views:
    1,133
    Daniel Vallstrom
    Mar 15, 2005
  4. beginner
    Replies:
    13
    Views:
    567
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    189
    7stud --
    Feb 20, 2008
Loading...

Share This Page