Again, Tucao: the data on the xml root level is invalid. Line 1, position 1

Keywords: xml encoding Google IE

Links to the original text: http://www.cnblogs.com/XChWaad/p/3346875.html

First, I write an XML in memory using xmlTextWriter. The method is as follows:

        private byte[] GetSendByte(List<Article> returnList)
        {  // Create xml to string in memory
            using (MemoryStream fileStream = new MemoryStream())
            {
                using (XmlTextWriter textWriter = new XmlTextWriter(fileStream, Encoding.UTF8))
                {
                    textWriter.Formatting = Formatting.Indented;
                    textWriter.WriteStartDocument();
                    textWriter.WriteStartElement("Doc");
                    textWriter.WriteStartElement("ArrayOfArticle");
                    foreach (var item in returnList)
                    {
                        textWriter.WriteStartElement("Article");
                        textWriter.WriteElementString("Aid", item.Aid);
                        textWriter.WriteElementString("ATitle", item.ATitle);
                        textWriter.WriteElementString("ACatalog", item.ACatalog);
                        textWriter.WriteElementString("CatalogName", item.CatalogName);
                        textWriter.WriteElementString("AContent", item.AContent);
                        textWriter.WriteElementString("AddTime", item.AddTime);
                        textWriter.WriteElementString("SendTime", item.SendTime);
                        textWriter.WriteEndElement();
                    }
                    textWriter.WriteEndElement();//end  ArrayOfArticle
                    textWriter.WriteElementString("LastGetTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    textWriter.WriteEndElement();//end  body

                    textWriter.WriteEndDocument();//end Document

                    textWriter.Close();
                }
                return fileStream.ToArray();
            }
            //throw new NotImplementedException();
        }

Converting byte [] to string yields the following xml:

 

<?xml version="1.0" encoding="utf-8"?>
<body>
  <ArrayOfArticle>
    <Article>
      <Aid>246</Aid>
      <ATitle>222</ATitle>
      <ACatalog>12</ACatalog>
      <CatalogName>12</CatalogName>
      <AContent>2222</AContent>
      <AddTime>2013-09-30 09:52:41</AddTime>
      <SendTime>2013/9/30 9:52:41</SendTime>
    </Article>
    <Article>
      <Aid>738</Aid>
      <ATitle>1111</ATitle>
      <ACatalog>12</ACatalog>
      <CatalogName>12</CatalogName>
      <AContent>11111</AContent>
      <AddTime>2013-09-30 09:52:37</AddTime>
      <SendTime>2013/9/30 9:52:36</SendTime>
    </Article>
  </ArrayOfArticle>
  <LastGetTime>2013-09-30 09:52:46</LastGetTime>
</body>

String absolute original, word for word.

Then x.LoadXml(xmlstring);

Nima, directly gave me a "data at the root level is invalid. Line 1, position 1".

Looking at xml carefully, I can't find any problems.

Then trace the string, the hair contains "r\n" and the space, guess it is the trouble they caused, resolutely Replace. The result is useless. Still make a mistake.

Then all kinds of google,

According to the. net version, just change. net 4.0 to 2.0. When I look at it, I'm 2.0.

Some people say that the XML format is incorrect. Obviously, there is nothing wrong with this xml.

Later, I saw a brother's article, the original text is very simple, directly pasted up:

http://hi.baidu.com/xsharkx/item/d2da2935cb6995f6a88428cc

Invalid row 1 position 1 problem for data at root level resolves reference to public void Parse(string xml)
   {
    this.Items=new RssItemCollection();
    XmlDocument xDoc=new XmlDocument();
    try
    {
     xDoc.LoadXml(xml);
    }
    catch(Exception e)
    {
     throw new Exception("content does not conform to standards", e);
    }
} 

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

After searching the Internet for a long time without results, I finally tried it myself. Load added xml=xml.Trim(); passed, but there were no strange errors such as blanks before and after xml, and the loaded XML was not wrong under IE.

Don't dare to look forward to the same experience for reference

Decisive trim(), I fuck, all right! And look back, before and after trim() it's exactly the same.

Now leave the question to you, why???

Reprinted at: https://www.cnblogs.com/XChWaad/p/3346875.html

Posted by djcubez on Fri, 26 Jul 2019 23:56:07 -0700