1

Following the instructions in this post, I added code to my program to get rid of any BOM that might be hiding at the start of my XML file. Even so, I'm still experiencing the error

'Data at Root Level is invalid' Line='1' Position='1'

At this point, all I'm trying to do is test a simple web service, which connects to one of the companies with which mine does business. They need to receive an XML file, which, if created correctly, should get me a response, which I can parse later. I've confirmed with their rep that the XML file, as it stands, should work correctly, so I have to assume the issue is with my POST operation. My code:

public string testOperation(string xmlDocLoc)
{
    try
    {
        XmlDocument doc = new XmlDocument();
        XmlDocument parcel = new XmlDocument();
        doc.Load(xmlDocLoc);

        string un = [removed];
        string pw = [removed];
        string combine = un + ":" + pw;
        string b64UCP = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(combine));

        string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());

        string xmlAsString = doc.OuterXml;
        if (xmlAsString.StartsWith(_byteOrderMarkUtf8))
        {
            var lastIndex = _byteOrderMarkUtf8.Length - 1;
            xmlAsString = xmlAsString.Remove(0, lastIndex);
            parcel.LoadXml(xmlAsString);
        }
        else
        {
            parcel = doc;
        }

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create([removed]);
        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(parcel.ToString());
        request.ContentType = "text/xml; encoding='utf-8'";
        request.ContentLength = bytes.Length;
        request.Method = "POST";
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();
        request.Headers.Add("Authorization", "Basic " +  b64UCP);
        HttpWebResponse response;
        response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream responseStream = response.GetResponseStream();
            string responseStr = new StreamReader(responseStream).ReadToEnd();
            return responseStr;
        }
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
    return null;

What, if anything, would be causing this code to throw this error?

Community
  • 1
  • 1
Inagnikai
  • 281
  • 1
  • 15
  • 3
    What might help is a line where the exception is being generated and as well as your first line of the XML. You are loading xml twice in this code and that exception could be thrown at either location. If its being throw on your first load, your removal code is not actually getting called. – Bearcat9425 Feb 23 '17 at 14:47
  • The error I'm getting is being returned from the remote service - it's owned by the other company, not by me. In this instance, after debugging, it's showing that the conditional to check for the BOM isn't actually being called. This means that "doc" is what is being POSTed through the HttpWebRequest. The code runs all the way up to "return responseStr", which is when it returns the exception from the remote service. – Inagnikai Feb 23 '17 at 15:30
  • 1
    I think the issue is XmlDocument.ToString() isnt doing what you think its doing. parcel.ToString() will give you "System.Xml.Document". Essentially you are just sending up to the service the string "System.Xml.Document". You need to use the code here to get the string contents of your XML document. http://stackoverflow.com/questions/2407302/convert-xmldocument-to-string – Bearcat9425 Feb 23 '17 at 22:19
  • Did that resolve your issue? – Bearcat9425 Feb 24 '17 at 20:11
  • It did! Thank you for your help! I don't want to take credit for the answer from you. Do you mind reposting your comment as an answer so I can mark this solved? – Inagnikai Feb 27 '17 at 17:15

2 Answers2

2

Looks like you are passing into your request the parcel.ToString() method however this is not correctly outputting the parcel xml but instead it is simply putting the string "System.Xml.Document" into your request. You would need to access the true xml of the XmlDocument but utilizing one of the methods provided in this answer here, Convert XmlDocument to String

Community
  • 1
  • 1
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
0

Use simpler way to get rid of bom:

var noBom = bom.Replace("\ufeff", string.Empty);
j.v.
  • 977
  • 6
  • 15