How to get the "raw" XML document returned from a web service

Discussion in 'ASP .Net Web Services' started by MikeL, Feb 17, 2005.

  1. MikeL

    MikeL Guest

    Hello.

    I am consuming a web service by adding a web reference to the service's WSDL
    file, and everything works fine.

    When I call the service I need to also "grab" the "raw" XML document that
    the service returned.

    How do I go about this? Do I use the XMLSerializer?

    Thanks in advance,

    Mike
     
    MikeL, Feb 17, 2005
    #1
    1. Advertising

  2. Mike you should be able to pass the XmlElement object without a
    problem.

    [WebMethod]
    public System.Xml.XmlElement MyMethod()
    {
    //some code to get XmlElement
    return myXmlElement; //System.Xml.XmlElement
    }
     
    Keenan Newton, Feb 17, 2005
    #2
    1. Advertising

  3. MikeL

    MikeL Guest

    Hi, Keenan. Thanks for the response.

    The problem is on the consumer of the web service. They want the XML
    document.

    I don't want to write another WebMethod just for this. I'd like to know how
    to serialize the returned data to an XML doc. The following code will do it
    and save it to disk, but ultimately I want not to write it to disk, but
    rather store it in a "string" variable:

    System.Xml.Serialization.XmlSerializer s = new
    System.Xml.Serialization.XmlSerializer(typeof(seResults),https://www.somedomain.com/RatingHub);

    Stream fs = new FileStream("D:\\stream.txt",
    FileMode.Truncate );

    XmlWriter writer = new XmlTextWriter(fs, new
    UTF8Encoding());

    s.Serialize(writer, results);

    Any thoughts?

    Thanks again,

    Mike


    "Keenan Newton" <> wrote in message
    news:...
    > Mike you should be able to pass the XmlElement object without a
    > problem.
    >
    > [WebMethod]
    > public System.Xml.XmlElement MyMethod()
    > {
    > //some code to get XmlElement
    > return myXmlElement; //System.Xml.XmlElement
    > }
    >
     
    MikeL, Feb 17, 2005
    #3
  4. OK you need to find out what object type they are expecting on their
    end if it a string or XMlNode, or XmlElement, typically a XmlNode will
    accept a XmlDocument or XmlElement. An XmlElement is essentially or it
    can be the Xml within an Xmldocument. the XmlDocument class has a
    property called DocumentElement. This is essentially all the XML in a
    "XmlDocument" The DocumentElement if you look at it really is of type
    XmlElement. So if they want it as a string of Xml you again can use
    the XmlElement, and return the OuterXml element. So First things first
    find out the exact data type they want XmlDocument, XmlNode, XmlElement
    or string. Oh yes don't use the filestream use a MemoryStream if
    possible
     
    Keenan Newton, Feb 18, 2005
    #4
  5. "MikeL" <> wrote in message
    news:e$...
    > Hello.
    >
    > I am consuming a web service by adding a web reference to the service's
    > WSDL file, and everything works fine.
    >
    > When I call the service I need to also "grab" the "raw" XML document that
    > the service returned.
    >
    > How do I go about this? Do I use the XMLSerializer?
    >
    > Thanks in advance,
    >


    You can use a Soap extension to log the SOAP to a file. Check out
    http://msdn.microsoft.com/library/d...bservicesprotocolssoapextensionclasstopic.asp
    (watch for the wrap).

    To hook the SoapExtension in the consumer application, compile the code in
    the article to TraceSoapExtension.dll and add this to the consumer's config
    file:

    <system.web>
    <webServices>
    <soapExtensionTypes>
    <add type="TraceExtension, TraceSoapExtension" priority="1"
    group="0" />
    </soapExtensionTypes>
    </webServices>
    </system.web>

    Regards,
    Sami
     
    Sami Vaaraniemi, Feb 18, 2005
    #5
  6. MikeL

    MikeL Guest

    Hello, Sami. Thanks for the response.

    I figured out a way to do it that looks like the following. Can you think of
    why this is not a good way to do it? A call to the web service has been made
    and the returned "seResutls" object assigned to variable "results":

    ___________________________________________________________________________________________
    System.Xml.Serialization.XmlSerializer s = new
    System.Xml.Serialization.XmlSerializer(typeof(seResults));
    MemoryStream mm = new MemoryStream();

    // Now serialize it:
    s.Serialize(mm, results);

    // Now convert it to a string:
    ASCIIEncoding encoding = new ASCIIEncoding( );
    string str = encoding.GetString(mm.GetBuffer());
    str = str.Trim();
    MessageBox.Show(str);
    ___________________________________________________________________________________________


    Thanks again,

    Mike

    "Sami Vaaraniemi" <> wrote in message
    news:cv46rm$61a$...
    >
    > "MikeL" <> wrote in message
    > news:e$...
    >> Hello.
    >>
    >> I am consuming a web service by adding a web reference to the service's
    >> WSDL file, and everything works fine.
    >>
    >> When I call the service I need to also "grab" the "raw" XML document that
    >> the service returned.
    >>
    >> How do I go about this? Do I use the XMLSerializer?
    >>
    >> Thanks in advance,
    >>

    >
    > You can use a Soap extension to log the SOAP to a file. Check out
    > http://msdn.microsoft.com/library/d...bservicesprotocolssoapextensionclasstopic.asp
    > (watch for the wrap).
    >
    > To hook the SoapExtension in the consumer application, compile the code in
    > the article to TraceSoapExtension.dll and add this to the consumer's
    > config file:
    >
    > <system.web>
    > <webServices>
    > <soapExtensionTypes>
    > <add type="TraceExtension, TraceSoapExtension" priority="1"
    > group="0" />
    > </soapExtensionTypes>
    > </webServices>
    > </system.web>
    >
    > Regards,
    > Sami
    >
     
    MikeL, Feb 18, 2005
    #6
  7. You are re-serializing the 'results' object in the consumer, right? This is
    not the same as obtaining the original XML from the Soap message, but I
    guess it's ok if all you want is an XML representation of your object. Keep
    in mind that the original XML and the re-serialized version might not be
    identical.

    Regards,
    Sami

    "MikeL" <> wrote in message
    news:...
    > Hello, Sami. Thanks for the response.
    >
    > I figured out a way to do it that looks like the following. Can you think
    > of why this is not a good way to do it? A call to the web service has been
    > made and the returned "seResutls" object assigned to variable "results":
    >
    > ___________________________________________________________________________________________
    > System.Xml.Serialization.XmlSerializer s = new
    > System.Xml.Serialization.XmlSerializer(typeof(seResults));
    > MemoryStream mm = new MemoryStream();
    >
    > // Now serialize it:
    > s.Serialize(mm, results);
    >
    > // Now convert it to a string:
    > ASCIIEncoding encoding = new ASCIIEncoding( );
    > string str = encoding.GetString(mm.GetBuffer());
    > str = str.Trim();
    > MessageBox.Show(str);
    > ___________________________________________________________________________________________
    >
    >
    > Thanks again,
    >
    > Mike
    >
    > "Sami Vaaraniemi" <> wrote in message
    > news:cv46rm$61a$...
    >>
    >> "MikeL" <> wrote in message
    >> news:e$...
    >>> Hello.
    >>>
    >>> I am consuming a web service by adding a web reference to the service's
    >>> WSDL file, and everything works fine.
    >>>
    >>> When I call the service I need to also "grab" the "raw" XML document
    >>> that the service returned.
    >>>
    >>> How do I go about this? Do I use the XMLSerializer?
    >>>
    >>> Thanks in advance,
    >>>

    >>
    >> You can use a Soap extension to log the SOAP to a file. Check out
    >> http://msdn.microsoft.com/library/d...bservicesprotocolssoapextensionclasstopic.asp
    >> (watch for the wrap).
    >>
    >> To hook the SoapExtension in the consumer application, compile the code
    >> in the article to TraceSoapExtension.dll and add this to the consumer's
    >> config file:
    >>
    >> <system.web>
    >> <webServices>
    >> <soapExtensionTypes>
    >> <add type="TraceExtension, TraceSoapExtension" priority="1"
    >> group="0" />
    >> </soapExtensionTypes>
    >> </webServices>
    >> </system.web>
    >>
    >> Regards,
    >> Sami
    >>

    >
    >
     
    Sami Vaaraniemi, Feb 18, 2005
    #7
  8. Sami also has a good idea but the code saves it to a file, not sure if
    that is what you really want to do or not
     
    Keenan Newton, Feb 18, 2005
    #8
  9. This should do what you want.

    /// <summary>
    /// Converts an object to its xml representation
    /// </summary>
    public static string ObjectToXml(object objectToConvert)
    {
    MemoryStream memStream = new MemoryStream();
    XmlSerializer serializer = new XmlSerializer(objectToConvert.GetType());
    serializer.Serialize(memStream, objectToConvert);
    StreamReader reader = new StreamReader(memStream);
    memStream.Position = 0;
    return reader.ReadToEnd();
    }

    -Trevor



    Hello MikeL,

    > Hi, Keenan. Thanks for the response.
    >
    > The problem is on the consumer of the web service. They want the XML
    > document.
    >
    > I don't want to write another WebMethod just for this. I'd like to
    > know how to serialize the returned data to an XML doc. The following
    > code will do it and save it to disk, but ultimately I want not to
    > write it to disk, but rather store it in a "string" variable:
    >
    > System.Xml.Serialization.XmlSerializer s
    > = new
    > System.Xml.Serialization.XmlSerializer(typeof(seResults),https://www.s
    > omedomain.com/RatingHub);
    >
    > Stream fs = new
    > FileStream("D:\\stream.txt", FileMode.Truncate );
    >
    > XmlWriter writer = new XmlTextWriter(fs,
    > new UTF8Encoding());
    >
    > s.Serialize(writer, results);
    >
    > Any thoughts?
    >
    > Thanks again,
    >
    > Mike
    >
    > "Keenan Newton" <> wrote in message
    > news:...
    >
    >> Mike you should be able to pass the XmlElement object without a
    >> problem.
    >>
    >> [WebMethod]
    >> public System.Xml.XmlElement MyMethod()
    >> {
    >> //some code to get XmlElement
    >> return myXmlElement; //System.Xml.XmlElement
    >> }
     
    Trevor Pinkney, Feb 21, 2005
    #9
  10. MikeL

    Dan Rogers Guest

    Do you want to just see it (for learning/teaching?) or are you saying you
    want the caller to always get back an XML document?

    If this latter is the case, use MSXML to format your own SOAP XML request,
    and use the HTTPPost method to post the document that represents the
    request to the service endpoint URL. Then take the output, strip out the
    HTTP header, and then load the remainder into an XML DOM (or just hand the
    client the stream and let them party on the raw html/xml.)

    --------------------
    >From: "MikeL" <>
    >Subject: How to get the "raw" XML document returned from a web service
    >Date: Thu, 17 Feb 2005 15:02:38 -0500
    >Lines: 15
    >X-Priority: 3
    >X-MSMail-Priority: Normal
    >X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
    >X-RFC2646: Format=Flowed; Original
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
    >Message-ID: <e$>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >NNTP-Posting-Host: 208.28.116.33
    >Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    >Xref: TK2MSFTNGXA01.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:5607
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >Hello.
    >
    >I am consuming a web service by adding a web reference to the service's

    WSDL
    >file, and everything works fine.
    >
    >When I call the service I need to also "grab" the "raw" XML document that
    >the service returned.
    >
    >How do I go about this? Do I use the XMLSerializer?
    >
    >Thanks in advance,
    >
    >Mike
    >
    >
    >
     
    Dan Rogers, Mar 8, 2005
    #10
  11. MikeL

    Dan Rogers Guest

    It won't give him the SOAP request or the HTTP headers - but it's close.
    --------------------
    >Message-ID: <>
    >From: Trevor Pinkney <>
    >Subject: Re: How to get the "raw" XML document returned from a web service
    >References: <>
    >Content-Type: text/plain; charset=iso-8859-1; format=flowed
    >X-Newsreader: JetBrains Omea Reader 439.21
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >Date: Mon, 21 Feb 2005 11:20:40 -0800
    >NNTP-Posting-Host: z-f5-0-0-229-s1.gw1.tor1.sprint-canada.net

    206.186.91.250
    >Lines: 1
    >Path:

    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
    4.phx.gbl
    >Xref: TK2MSFTNGXA02.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:28225
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >This should do what you want.
    >
    > /// <summary>
    > /// Converts an object to its xml representation
    > /// </summary>
    > public static string ObjectToXml(object objectToConvert)
    > {
    > MemoryStream memStream = new MemoryStream();
    > XmlSerializer serializer = new XmlSerializer(objectToConvert.GetType());
    > serializer.Serialize(memStream, objectToConvert);
    > StreamReader reader = new StreamReader(memStream);
    > memStream.Position = 0;
    > return reader.ReadToEnd();
    > }
    >
    >-Trevor
    >
    >
    >
    >Hello MikeL,
    >
    >> Hi, Keenan. Thanks for the response.
    >>
    >> The problem is on the consumer of the web service. They want the XML
    >> document.
    >>
    >> I don't want to write another WebMethod just for this. I'd like to
    >> know how to serialize the returned data to an XML doc. The following
    >> code will do it and save it to disk, but ultimately I want not to
    >> write it to disk, but rather store it in a "string" variable:
    >>
    >> System.Xml.Serialization.XmlSerializer s
    >> = new
    >> System.Xml.Serialization.XmlSerializer(typeof(seResults),https://www.s
    >> omedomain.com/RatingHub);
    >>
    >> Stream fs = new
    >> FileStream("D:\\stream.txt", FileMode.Truncate );
    >>
    >> XmlWriter writer = new XmlTextWriter(fs,
    >> new UTF8Encoding());
    >>
    >> s.Serialize(writer, results);
    >>
    >> Any thoughts?
    >>
    >> Thanks again,
    >>
    >> Mike
    >>
    >> "Keenan Newton" <> wrote in message
    >> news:...
    >>
    >>> Mike you should be able to pass the XmlElement object without a
    >>> problem.
    >>>
    >>> [WebMethod]
    >>> public System.Xml.XmlElement MyMethod()
    >>> {
    >>> //some code to get XmlElement
    >>> return myXmlElement; //System.Xml.XmlElement
    >>> }

    >


    >
    >
     
    Dan Rogers, Mar 8, 2005
    #11
    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. Jesse Liberty

    Displaying XML (raw) from an ASP.NET Document

    Jesse Liberty, Aug 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    546
    Jesse Liberty
    Aug 22, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    760
    Tony Prichard
    Dec 12, 2003
  3. Chris Hayes
    Replies:
    1
    Views:
    220
    Marc Landrum
    Jun 1, 2005
  4. Ara.T.Howard
    Replies:
    2
    Views:
    130
  5. icogs
    Replies:
    0
    Views:
    150
    icogs
    Jan 31, 2008
Loading...

Share This Page