这可以很好地工作:
XDocument xdoc = new XDocument(
new XDeclaration("1.1", "UTF-8", "yes"),
new XProcessingInstruction("foo", "bar"),
new XElement("test"));
但是,如果我将其更改为显式地将"params array“作为数组传递:
object[] content = new object[] {
new XDeclaration("1.1", "UTF-8", "yes"),
new XProcessingInstruction("foo", "bar"),
new XElement("test")
};
xdoc = new XDocument(content);
它会失败,错误为:
System.ArgumentException:无法将非空白字符添加到内容中。
这两个例子不是完全等价的吗?这里发生了什么事?
发布于 2009-09-17 13:42:40
当您使用第一种方法时,您使用的是XDocument的重载,该重载首先接受XDeclaration,然后接受内容的参数。然而,当您使用第二种方法时,您使用的是带参数的内容的重载。object[]数组中的XDeclaration是作为内容传入的,这就是它爆炸的地方。
查看此处:http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.xdocument.aspx
发布于 2011-09-20 05:27:01
如果使用XDocument
构造函数而不是工厂方法,则在分析XML字符串时可能会出现此错误。
给定:
var xmlString = "<some-xml />";
这将失败:
var doc = new XDocument(xmlString);
这是可行的:
var doc = XDocument.Parse(xmlString);
https://stackoverflow.com/questions/1438935
复制相似问题