Wim*_*nen 17 .net c# linq-to-xml
这很好用:
XDocument xdoc = new XDocument(
new XDeclaration("1.1", "UTF-8", "yes"),
new XProcessingInstruction("foo", "bar"),
new XElement("test"));
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其更改为将"params数组"显式传递为数组:
object[] content = new object[] {
new XDeclaration("1.1", "UTF-8", "yes"),
new XProcessingInstruction("foo", "bar"),
new XElement("test")
};
xdoc = new XDocument(content);
Run Code Online (Sandbox Code Playgroud)
它失败了:
System.ArgumentException:无法将非空白字符添加到内容中.
这两个例子不完全相同吗?这里发生了什么?
Dre*_*kes 21
如果使用XDocument构造函数而不是工厂方法,则在解析XML字符串时可能会出现此错误.
鉴于:
var xmlString = "<some-xml />";
Run Code Online (Sandbox Code Playgroud)
这失败了:
var doc = new XDocument(xmlString);
Run Code Online (Sandbox Code Playgroud)
这有效:
var doc = XDocument.Parse(xmlString);
Run Code Online (Sandbox Code Playgroud)
BFr*_*ree 14
当你使用第一种方法时,你正在使用XDocument的重载,它首先采用XDeclaration,然后是内容的参数.但是,当您使用第二种方法时,您正在使用带有内容的参数的重载.你的object []数组中的XDeclaration是作为内容传播的,而这就是它爆炸的地方.
请看:http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.xdocument.aspx