错误: – XmlReader状态应该是XDocument.Load上的Interactive
我收到以下错误: –
System.InvalidOperationException:XmlReader状态应为Interactive。 System.Xml.Linq.XDocument.Load(XmlReader reader,LoadOptions选项)中的System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r,LoadOptions o)
在以下代码中。 有谁能指出我在这里做错了什么?
static XDocument GetContentAsXDocument(string xmlData) { XmlDocument xmlDocument = new XmlDocument(); if (!string.IsNullOrEmpty(xmlData)) { xmlDocument.LoadXml(xmlData); return xmlDocument.ToXDocument(); } else { return new XDocument(); } } /// /// Converts XMLDocument to XDocument /// /// /// 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)); } }
您应该使用XDocument.Parse和XmlDocument.OuterXml 。 请参阅下面的示例。
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(); } }
可以在此处找到从XmlDocument转换为XDocument的其他方法。
上述就是C#学习教程:错误: – XmlReader状态应该是XDocument.Load上的Interactive分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/984698.html