4

In C#, I am trying to get call a webservice which returns an XML file.

I can make a HttpWebRequest to the webservice and store the output in a StreamReader. But how can I convert this data into an XMLDocument?

Troyes22
  • 43
  • 1
  • 1
  • 3
  • Is this a SOAP-based web service? Then you should not be using HttpWebRequest. Just use "Add Service Reference" and call the "methods" of the proxy class that is created for you - no XML involved. – John Saunders Nov 24 '10 at 21:51

3 Answers3

13

Use XmlDocument.Load() - I'm using the overload that accepts an XmlReader to cash in on XmlReader.Create's auto-encoding detection:

XmlDocument document = new XmlDocument();
using(Stream stream = request.GetResponse().GetResponseStream()) {        
    using(XmlReader reader = XmlReader.Create(stream)) {
        document.Load(stream);
    }
}
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
2

Use System.Xml.Linq.XDocument.Load(streamreader);

dthorpe
  • 35,318
  • 5
  • 75
  • 119
0

You should store the received output in a StringWriter or just a stringand the load it using XmlDocument.Load(string).

florin
  • 405
  • 4
  • 20