System.Xml.XmlException:意外的XML声明. XML声明必须是第一个 [英] System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first

查看:628
本文介绍了System.Xml.XmlException:意外的XML声明. XML声明必须是第一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑并保存后将字符串解析到XDocument时出现此错误.但是任何人都可以帮助我找到错误的位置-第1行,位置10475.如何获得该位置???

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:意外的XML声明. XML 声明必须是文档中的第一个节点,并且不能有空格 允许在其前面出现字符.第1行,位置10475.

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(); 

XML很大,这是它的一部分

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>

推荐答案

请确保您的<?xml标记是文档中的第一件事(并且在此之前没有任何内容,包括空格). 每个文档只能有一个<?xml,因此,如果您有大量的XML,并且在行中的某个位置重复了此标签,则该文档将无效.

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.

这篇关于System.Xml.XmlException:意外的XML声明. XML声明必须是第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆