簡體   English   中英

錯誤: - XmlReader狀態應該是XDocument.Load上的Interactive

[英]Error :- The XmlReader state should be Interactive on XDocument.Load

我收到以下錯誤: -

System.InvalidOperationException:XmlReader狀態應為Interactive。 System.Xml.Linq.XDocument.Load上的System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r,LoadOptions o)(XmlReader reader,LoadOptions選項)

在以下代碼中。 有誰能指出我在這里做錯了什么?

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

您應該使用XDocument.ParseXmlDocument.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的其他方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM