Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

c# - System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first

I got this error while Parse an string to XDocument after edit and save it. But anyone can help me locate error position - The Line 1, position 10475. How can i get that position ???

System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 10475.

if (storage.FileExists("APPSDATA.xml"))
{
  var reader = new StreamReader(new IsolatedStorageFileStream("APPSDATA.xml", FileMode.Open, storage));

  string xml = reader.ReadToEnd();
  var xdoc = XDocument.Parse(xml);//error here 
  reader.Close(); 

The XML is big, this is jus a part of it

<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
  <Ungdung>
    <Name>HERE City Lens</Name>
    <Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
    <Path>/Icon/herecitylens.png</Path>
    <Version>1.0.0.0</Version>
    <Category>HERE</Category>
    <Date>Uknown</Date>
  </Ungdung>
  <Ungdung>
    <Name>HERE Transit</Name>
    <Id>adfdad16-b54a-4ec3-b11e-66bd691be4e6</Id>
    <Path>/Icon/heretransit.png</Path>
    <Version>1.0.0.0</Version>
    <Category>HERE</Category>
    <Date>Uknown</Date>
  </Ungdung>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure your <?xml tag is the first thing in the document (and that it doesn't have anything before that, this includes whitespace). You can have <?xml only once per document, so if you have a large chunk of XML and you have this tag repeated somewhere down the lines your document won't be valid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...