Saturday, December 26, 2009

The XmlReader state should be Interactive

@YaronNaveh

Recently I was parsing Xml in c# from an XmlReader to an XElement:


MemoryStream m = GetMemoryStream();
XmlReader r = XmlReader.Create(m);
XElement e = XElement.ReadFrom(r) as XElement;


The last line threw this exception:


System.InvalidOperationException
The XmlReader state should be Interactive.


In MSDN ReadState.Interactive state is described as:


The Read method has been called. Additional methods may be called on the reader.


But why should I call read? It is the XElement responsibility to do it.

The solution is pretty simple though:


MemoryStream m = GetMemoryStream();
XmlReader r = XmlReader.Create(m);
//This is how we make the XmlReader interactive
r.MoveToContent();

XElement e = XElement.ReadFrom(r) as XElement;

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

1 comments:

Sam said...

For me the solution was to use XElement.Load instead of XElement.ReadFrom.