尝试读取带有嵌套 XML 对象和自己的 XML 声明的 XML 文件。正如预期的那样有异常: 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.
我如何将该特定元素作为文本读取并将其解析为单独的 XML 文档以供以后反序列化?
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Items>
<Item>
<Target type="System.String">Some target</Target>
<Content type="System.String"><?xml version="1.0" encoding="utf-8"?><Data><Items><Item><surname type="System.String">Some Surname</surname><name type="System.String">Some Name</name></Item></Items></Data></Content>
</Item>
</Items>
</Data>
由于声明异常,我尝试的每种方法都失败了。
var xml = System.IO.File.ReadAllText("Info.xml");
var xDoc = XDocument.Parse(xml); // Exception
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml); // Exception
var xmlReader = XmlReader.Create(new StringReader(xml));
xmlReader.ReadToFollowing("Content"); // Exception
我无法控制 XML 的创建。
繁星淼淼
慕斯王
相关分类