简体   繁体   English

什么是XML空属性,以及如何在Linq-To-XML中处理它们?

[英]What is a XML null attribute, and how to handle them in Linq-To-XML?

I'm trying to read a XmlReader into a XDocument 我正在尝试将XmlReader读入XDocument

//GetContentStructureReader() retrieves the reader from an external source
XmlReader reader = GetContentStructureReader();
XDocument.Load(reader);

I keep getting the following exception with one specific data source: 对于一个特定的数据源,我不断收到以下异常:

System.ArgumentNullException was unhandled by user code Message=Value cannot be null. 用户代码未处理System.ArgumentNullException Message = Value不能为null。 Parameter name: value Source=System.Xml.Linq ParamName=value StackTrace: at System.Xml.Linq.XAttribute..ctor(XName name, Object value) at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r) at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) at System.Xml.Linq.XDocument.Load(XmlReader reader) 参数名称:值Source = System.Xml.Linq ParamName =值StackTrace:位于System.Xml.Linq.XAttribute..ctor(位于System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)处System.Xml.Linq.XDocument.Load(XmlReader阅读器,LoadOptions选项)上的.Xml.Linq.XContainer.ReadContentFrom(XmlReader r,LoadOptions o)在System.Xml.Linq.XDocument.Load(XmlReader阅读器)

So it seems that during the loading, at some point a XAttribute is being initialized with null value. 因此,似乎在加载期间,某个XAttribute正在使用null值初始化。

What is a null attribute in XML? XML中的null属性是什么? I've tried examining the (6 megabyte) source document to correct the data, but in vain, since I don't know what kind of XML construct I'm looking for. 我尝试检查(6兆字节)源文档以更正数据,但由于我不知道要寻找哪种XML构造而徒劳无功。

Is there a workaround for this? 有没有解决方法?

During loading of a document null value should not appear as values for XAttribute. 在加载文档期间,空值不应显示为XAttribute的值。 So what you're seeing is unexpected. 因此,您所看到的是意外的。 You could stop on the exception in the debugger to see the callstack and the values of the parameters (the XName name in particular) which might help you locating the attribute in the source document. 您可以在调试器中停止该异常,以查看调用堆栈和参数值(特别是XName名称),这可能有助于您在源文档中定位属性。 (You could also try ((IXmlLineInfo)reader) in your whatch window on one of the frames which do have the reader defined. In any case the default implementation of XmlReader in the .NET Framework should never cause this. So the question is, where/how did you create the instance of the XmlReader object you pass to the Load method? (您也可以在what窗口中的确实定义了阅读器的其中一个框架上尝试[[(IXmlLineInfo)reader)。在任何情况下,.NET Framework中XmlReader的默认实现都不会导致这种情况。所以问题是,您在哪里/如何创建了传递给Load方法的XmlReader对象的实例?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM