Search code examples
c#asp.netxmlwcf

xml.LoadData Data at root level is invalid, line 1, pos 1. Existing solutions not helping


I am trying to save an xml file that I have built from a string to local memory in App_Data of a C# web service application. I am getting this error despite looking at previous stack solutions:

Data at the root level is invalid. Line 1, position 1.

The xml I am trying to save to a file is a simple list of nodes.

This is being tested through a ASP.NET application on a localhost. (could this potentially affect the web service application? I see no way for this to be possible though)

I have tried implementing the solutions from these two previous stack questions:

  1. First one
  2. Second One

Here is the first code I tried

XmlDocument doc = new XmlDocument();
var xmlWithEscapedCharacters = SecurityElement.Escape(finalXML); //finalXML is of type string

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if((xmlWithEscapedCharacters.StartsWith(_byteOrderMarkUtf8))
{
    var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length - 1;
    xmlWithEscapedCharacters = xmlWithEscapedCharacters.Remove(0, lastIndexOfUtf8);
}

doc.LoadXml(xmlWithEscapedCharacters);
doc.PreserveWhitespace = true;
doc.Save("Parks.xml");

Then I tried this from the second solution

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if(xmlWithEscapedCharacters[0] == '\ufeff')
{
    var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length - 1;
    xmlWithEscapedCharacters = xmlWithEscapedCharacters.Remove(0, lastIndexOfUtf8);
}

Then I even thought there could somehow be more than one BOM at the beginning of the file and tried

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
while (xmlWithEscapedCharacters[0] == '\ufeff')
{
    var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length - 1;
    xmlWithEscapedCharacters = xmlWithEscapedCharacters.Remove(0, lastIndexOfUtf8);
}

Which resulted in a time out error.

I am at a loss for what feels like a rather simple issue.

EDIT 2 (Wrong XML file, sorry): Here is an example of a valid XML file. I am adding a node with this code.

<Books>
<Book Cover="Paper back">
    <ISBN>978-0132309981</ISBN>
    <Title>Operating Systems Internals and Design Principles</Title>
    <Author>
        <Name>
            <First>William</First>
            <Last>Stallings</Last>
        </Name>
        <Contact Office="BY101">
            <Phone>480-965-0000</Phone>
        </Contact>
    </Author>
    <Publisher>Prentice Hall</Publisher>
    <Year Edition="7">2011</Year>
</Book>
</Books>

Solution

  • "Data at root level is invalid, line 1, pos 1" is usually caused by 1.XML files are not encoded in UTF-8 2.The XML file has BOM characters.

    I think you can check if there have BOM characters after replacing invalid characters with valid characters using the SecurityElement.Escape(String) method.

     public string UTF8Str(string str)
            {
               
                byte[] buffer = File.ReadAllBytes(str);
             
                byte[] bomBuffer = { 0xef, 0xbb, 0xbf };
                if ((buffer[0] == bomBuffer[0])&& (buffer[1] == bomBuffer[1])&&(buffer[2] == bomBuffer[2]))
    
                { 
                    var result = new UTF8Encoding(false).GetString(buffer);
                    result = "<" + result.Substring(result.IndexOf('<') + 1);
                    return result;
                }
                return Encoding.UTF8.GetString(buffer);
            })