views:

263

answers:

3

I need to post raw xml to a site and read the response. With the following code I keep getting an "Unknown File Format" error and I'm not sure why.

            XmlDocument sampleRequest = new XmlDocument();
            sampleRequest.Load(@"C:\SampleRequest.xml");
            byte[] bytes = Encoding.UTF8.GetBytes(sampleRequest.ToString());

            string uri = "https://www.sample-gateway.com/gw.aspx";
            req = WebRequest.Create(uri);
            req.Method = "POST";
            req.ContentLength = bytes.Length;
            req.ContentType = "text/xml";

            using (var requestStream = req.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }

            // Send the data to the webserver
            rsp = req.GetResponse();

            XmlDocument responseXML = new XmlDocument();
            using (var responseStream = rsp.GetResponseStream())
            {
                responseXML.Load(responseStream);
            }

I am fairly certain my issue is what/how I am writing to the requestStream so..

How can I modify that code so that I may write an xml located on the hard drive to the request stream?

+2  A: 

Hi, ok instead of doing sampleRequest.ToString(), you should use sampleRequest.OuterXml, and that would do the magic, you were sending "System.Xml.XmlDocument" instead of the Xml

    XmlDocument sampleRequest = new XmlDocument();
    sampleRequest.Load(@"C:\SampleRequest.xml");
    //byte[] bytes = Encoding.UTF8.GetBytes(sampleRequest.ToString());
    byte[] bytes = Encoding.UTF8.GetBytes(sampleRequest.OuterXml);
jjchiw
A: 

Two things:

First, whenever you're trying to diagnose a problem with an HTML response, you should always examine what the response stream actually contains. If you had in this case, you would have seen that it contains System.Xml.XmlDocument, which would have told you what was wrong pretty much immediately.

Second, in an application with any kind of transaction volume, you're not going to want to load a static XML file into an XmlDocument before putting it in the response stream; your program's spending time and memory building something that you don't need. (It's even worse than that in your case; your approach not only parses the XML into a DOM object, it then makes an in-memory copy of its OuterXml property when you encode it as UTF-8. Also, do you really need to be doing that?) Instead, you should create a FileStream object and use one of the techniques in this answer to copy it to the response stream.

Robert Rossney
I appreciate all the info - I'm still kind of a green programmer so forgive me if i say something stupid, but here goes: we aren't going to be loading a document from the hard drive we are building it in a separate method, but the method will return an XmlDocument, are you saying the method that builds the XmlDocument should return something other than an XmlDocument? what is the best way to get the XML into the stream in this case?
swolff1978
Oh, no, it's totally cool to build an XmlDocument in memory. The code you posted is loading the XmlDocument from disk, so that's what I assumed you were trying to do. If you've already got the XmlDocument object, I'd just write its OuterXml property to the response stream.
Robert Rossney
A: 

I've been unable to find an answer to this very question anywhere. I'm attempting to do the same thing: In VB.net take an xmlDocument and send it to a web address. This has to be something that's done all the time, yet I can't find it covered anywhere.

Tyler