【发布时间】:2013-09-03 23:38:47
【问题描述】:
在编辑并保存后将字符串解析到 XDocument 时出现此错误。但是任何人都可以帮我定位错误位置 - 第 1 行,位置 10475。我怎样才能得到那个位置?
System.Xml.XmlException:意外的 XML 声明。 XML 声明必须是文档中的第一个节点,并且不能有空格 允许字符出现在它之前。第 1 行,位置 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 很大,这只是其中的一部分
<?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>
【问题讨论】:
-
@BuiAkinori 正如
XDocument所说,您的 xml 中有问题。所以没有看到它,我只能说,用编辑器打开你的 xml,转到 xDocument 报告的位置,看看有什么问题。 -
@BuiAkinori
how to get to that poisition?真的吗?如果您遇到每一个小问题,您如何计划完成您的代码。 -
<?xml之前有什么吗? -
@BuiAkinori 空格呢?你有没有(在
<?xml之前)? -
@BuiAkinori 检查您是否在文档的其他任何地方重复了
<?xml(这是非法的,因此无效)。你说它很长,搜索它,看看你是否在某个地方找到它
标签: c# xml xml-parsing windows-phone-8 linq-to-xml