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?