错误: - 向XMLReader状态应该是XDocument.Load互动 [英] Error :- The XmlReader state should be Interactive on XDocument.Load
问题描述
我收到以下错误: -
System.InvalidOperationException:在
的XmlReader状态应该是互动的。
在
System.Xml.Linq.XContainer.ReadContentFrom(XmlReader中
R,LoadOptions O)在
System.Xml.Linq.XDocument.Load(XmlReader中
读卡器, LoadOptions选项)
块引用>
在下面的代码。任何人可以点我什么,我做错了什么?
静态的XDocument GetContentAsXDocument(字符串XMLDATA)
{
XmlDocument的XmlDocument的=新的XmlDocument();
如果(string.IsNullOrEmpty(XMLDATA)!)
{
xmlDocument.LoadXml(XMLDATA);
返回xmlDocument.ToXDocument();
}
,否则
{
返回新的XDocument();
}
}
///<总结>
///转换为XMLDocument到的XDocument
///< /总结>
///< PARAM NAME =XMLDOCUMENT>< /参数>
///<&回报GT;< /回报>使用
公共静态的XDocument ToXDocument(此XmlDocument的XmlDocument的)
{
(VAR nodeReader =新的XmlNodeReader对象(XMLDOCUMENT))
{
nodeReader.MoveToContent();
返回XDocument.Load(
nodeReader,
(LoadOptions.PreserveWhitespace |
LoadOptions.SetBaseUri |
LoadOptions.SetLineInfo));
}
}
解决方案您应该使用 XDocument.Parse 和< A HREF =http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.outerxml.aspx相对=nofollow> XmlDocument.OuterXml 。请参见下面的例子。
公共静态的XDocument ToXDocument(此XmlDocument的XmlDocument的)
{
串outerXml = xmlDocument.OuterXml;
如果
{
返回XDocument.Parse(outerXml,(LoadOptions.PreserveWhitespace(string.IsNullOrEmpty(outerXml)!)|
LoadOptions.SetBaseUri |
LoadOptions.SetLineInfo ));
}
,否则
{
返回新的XDocument();
}
}
的从XmlDocument的转换的XDocument可以在这里找到。
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)); } }
解决方案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.
这篇关于错误: - 向XMLReader状态应该是XDocument.Load互动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!