Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:-XDocument.Load上的XmlReader状态应为交互式_C#_Xml_Linq To Xml_Xmldocument - Fatal编程技术网

C# 错误:-XDocument.Load上的XmlReader状态应为交互式

C# 错误:-XDocument.Load上的XmlReader状态应为交互式,c#,xml,linq-to-xml,xmldocument,C#,Xml,Linq To Xml,Xmldocument,我得到以下错误:- System.InvalidOperationException:异常 XmlReader状态应该是交互式的。 在 System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r、 加载选项(o)在 System.Xml.Linq.XDocument.Load(XmlReader 读卡器,加载选项(可选) 在下面的代码中。有人能告诉我我做错了什么吗 static XDocument GetContentAsXDocument(st

我得到以下错误:-

System.InvalidOperationException:异常 XmlReader状态应该是交互式的。 在 System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r、 加载选项(o)在 System.Xml.Linq.XDocument.Load(XmlReader 读卡器,加载选项(可选)

在下面的代码中。有人能告诉我我做错了什么吗

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));
    }
}
static XDocument GetContentAsXDocument(字符串xmlData)
{
XmlDocument XmlDocument=新的XmlDocument();
如果(!string.IsNullOrEmpty(xmlData))
{
LoadXml(xmlData);
返回xmlDocument.ToXDocument();
}
其他的
{
返回新的XDocument();
}
}
/// 
///将XMLDocument转换为XDocument
/// 
/// 
/// 
公共静态XDocument文档(此XmlDocument XmlDocument)
{
使用(var nodeReader=新的XmlNodeReader(xmlDocument))
{
noderReader.MoveToContent();
返回XDocument.Load(
诺德瑞德,
(LoadOptions.preserveewhitespace)|
LoadOptions.SetBaseUri|
LoadOptions.SetLineInfo));
}
}
您应该使用和。请参见下面的示例

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();
    }
}

其他方法。

如果不调用MoveToContent会发生什么?“我现在不能自己测试它。”乔恩补充说,这是每个人都需要的。将返回并更新。@Jon,对MoveToContent()的任何原因调用都不应该存在吗?此错误只发生在少数XMLDocument对象上,而不是所有对象。@user7401431:Ah-如果事先知道它并不总是失败,那就好了。在这种情况下,您肯定应该查看MoveToContent的结果,并比较失败和工作情况下的值。在进行此操作时,您可以在MoveToContent()之后打印noderReader.ReadState。您是否有一个失败的示例文件,我尝试了一些文档,但都成功了。