12

I get the following error :-

System.InvalidOperationException: The XmlReader state should be Interactive. at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)

in the following code. Could anybody point me what I doing wrong here?

static XDocument GetContentAsXDocument(string xmlData)
{
    XmlDocument xmlDocument = new XmlDocument();
    if (!string.IsNullOrEmpty(xmlData))
    {
        xmlDocument.LoadXml(xmlData);
        return xmlDocument.ToXDocument();
    }
    else
    {
        return new XDocument();
    }
}


/// <summary>
///  Converts XMLDocument to XDocument
/// </summary>
/// <param name="xmlDocument"></param>
/// <returns></returns>
public static XDocument ToXDocument( this XmlDocument xmlDocument )
{
    using( var nodeReader = new XmlNodeReader( xmlDocument ) )
    {
        nodeReader.MoveToContent();
        return XDocument.Load(
             nodeReader,
            (LoadOptions.PreserveWhitespace |
             LoadOptions.SetBaseUri |
             LoadOptions.SetLineInfo));
    }
}
Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
  • What happens if you don't call MoveToContent? I can't test it myself at the moment. – Jon Skeet May 16 '11 at 16:03
  • @Jon, Added that Its needed per http://msdn.microsoft.com/en-us/library/bb538465.aspx. Will check back and update back. – Ashish Gupta May 16 '11 at 16:06
  • @Jon, any reason call to MoveToContent() should not be there? This error is happening for few XMLDocument objects not all. – Ashish Gupta May 16 '11 at 16:07
  • @user740431: Yup, found the same thing in the docs. Very strange. I suggest you print out the result of the MoveToContent to call just to check that there really is appropriate data... – Jon Skeet May 16 '11 at 16:08
  • 2
    @user7401431: Ah - it would have been nice to know that it wasn't always failing beforehand. In that case you should *definitely* look at the result of MoveToContent, and compare the value in failing and working situations. – Jon Skeet May 16 '11 at 16:10
  • 1
    While you are at it you could print nodeReader.ReadState after MoveToContent() Do you have a sample file that fails, I tried a few documents but it worked for all. – Johan Larsson Sep 30 '12 at 23:24
  • Just an FYI. I got this error trying to do XElement.ReadFrom() on an XmlReader returned from a stored procedure. Doing MoveToContent() places the reader in 'Interactive' state, and from there ReadFrom() correctly reads the XML. – redcalx Dec 23 '13 at 12:07

1 Answers1

3

You should use XDocument.Parse and XmlDocument.OuterXml. See the example below.

public static XDocument ToXDocument( this XmlDocument xmlDocument )
{
    string outerXml = xmlDocument.OuterXml;
    if(!string.IsNullOrEmpty(outerXml))
    {
        return XDocument.Parse(outerXml, (LoadOptions.PreserveWhitespace |
             LoadOptions.SetBaseUri |
             LoadOptions.SetLineInfo));
    }
    else
    {
        return new XDocument();
    }
}

Other methods of converting from XmlDocument to XDocument can be found here.

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90