jevakallio
jevakallio

Reputation: 35890

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

//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. 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)

So it seems that during the loading, at some point a XAttribute is being initialized with null value.

What is a null attribute in XML? 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.

Is there a workaround for this?

Upvotes: 0

Views: 1571

Answers (1)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

During loading of a document null value should not appear as values for 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. (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?

Upvotes: 1

Related Questions