我遇到了以下错误:
System.InvalidOperationException: XmlReader的状态应该是Interactive。 在以下代码中: System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
请问有谁能指出我在这里错了吗?
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));
}
}