简体   繁体   English

XDocument.Load(XmlReader)可能的异常

[英]XDocument.Load(XmlReader) Possible Exceptions

What are the possible exceptions that can be thrown when XDocument.Load(XmlReader) is called? 调用XDocument.Load(XmlReader)时可能抛出的异常是什么? It is hard to follow best practices (ie avoiding generic try catch blocks) when the documentation fails to provide crucial information. 当文档无法提供关键信息时,很难遵循最佳实践(即避免使用通用的try catch块)。

Thanks in advance for your assistance. 提前感谢你的帮助。

MSDN says: The loading functionality of LINQ to XML is built upon XmlReader.Therefore, you might catch any exceptions that are thrown by the XmlReader. MSDN说:LINQ to XML的加载功能是基于XmlReader构建的。因此,您可能会捕获XmlReader抛出的任何异常。 Create overload methods and the XmlReader methods that read and parse the document. 创建重载方法和读取和解析文档的XmlReader方法。

http://msdn.microsoft.com/en-us/library/756wd7zs.aspx ArgumentNullException and SecurityException http://msdn.microsoft.com/en-us/library/756wd7zs.aspx ArgumentNullException和SecurityException

EDIT: MSDN not always says true. 编辑:MSDN并不总是说真的。 So I've analyzed Load method code with reflector and got results like this: 所以我用反射器分析了Load方法代码并获得了如下结果:

public static XDocument Load(XmlReader reader)
{
    return Load(reader, LoadOptions.None);
}

Method Load is calling method: 方法Load是调用方法:

public static XDocument Load(XmlReader reader, LoadOptions options)
{
    if (reader == null)
    {
        throw new ArgumentNullException("reader"); //ArgumentNullException
    }
    if (reader.ReadState == ReadState.Initial)
    {
        reader.Read();// Could throw XmlException according to MSDN
    }
    XDocument document = new XDocument();
    if ((options & LoadOptions.SetBaseUri) != LoadOptions.None)
    {
        string baseURI = reader.BaseURI;
        if ((baseURI != null) && (baseURI.Length != 0))
        {
            document.SetBaseUri(baseURI);
        }
    }
    if ((options & LoadOptions.SetLineInfo) != LoadOptions.None)
    {
        IXmlLineInfo info = reader as IXmlLineInfo;
        if ((info != null) && info.HasLineInfo())
        {
            document.SetLineInfo(info.LineNumber, info.LinePosition);
        }
    }
    if (reader.NodeType == XmlNodeType.XmlDeclaration)
    {
        document.Declaration = new XDeclaration(reader);
    }
    document.ReadContentFrom(reader, options); // InvalidOperationException
    if (!reader.EOF)
    {
        throw new InvalidOperationException(Res.GetString("InvalidOperation_ExpectedEndOfFile")); // InvalidOperationException
    }
    if (document.Root == null)
    {
        throw new InvalidOperationException(Res.GetString("InvalidOperation_MissingRoot")); // InvalidOperationException
    }
    return document;
}

Lines with exceptions possibility are commented 具有例外可能性的行被评论

We could get the next exceptions:ArgumentNullException, XmlException and InvalidOperationException. 我们可以得到下一个异常:ArgumentNullException,XmlException和InvalidOperationException。 MSDN says that you could get SecurityException, but perhaps you can get this type of exception while creating XmlReader. MSDN说您可以获得SecurityException,但也许您可以在创建XmlReader时获得此类异常。

XmlReader.Create(Stream) allows two types of exceptions: [src] XmlReader.Create(Stream)允许两种类型的异常: [src]

XmlReader reader; // Do whatever you want

try
{
  XDocument.Load(reader);
}
catch (ArgumentNullException)
{
  // The input value is null.
}
catch (SecurityException)
{
  // The XmlReader does not have sufficient permissions 
  // to access the location of the XML data.
}
catch (FileNotFoundException)
{
  // The underlying file of the path cannot be found
}

Looks like the online documentation doesn't state which exceptions it throws... too bad. 看起来在线文档没有说明它抛出了哪些例外......太糟糕了。 You will save yourself some grief with FileNotFoundException's by using a FileInfo instance, and calling its Exists method to make sure the file really is there. 使用FileInfo实例,并通过调用其Exists方法确保文件确实存在 ,您将使用FileNotFoundException来避免一些悲伤。 That way you won't have to catch that type of exception. 这样你就不必捕获那种类型的异常。 [Edit] Upon re-reading your post, I forgot to notice that you are passing in an XML Reader. [编辑]重新阅读你的帖子后,我忘了你注意到你正在传入一个XML阅读器。 My response was based upon passing in a string that represents a file (overloaded method). 我的回复是基于传递代表文件的字符串(重载方法)。 In light of that, I would (like the other person who responded to your question had a good answer too). 鉴于此,我会(就像回答你的问题的另一个人也有一个很好的答案)。

In light of the missing exception list, I would suggest to make a test file with malformed XML and try loading it to see what type of exceptions really do get thrown. 鉴于缺少的异常列表,我建议使用格式错误的XML制作一个测试文件并尝试加载它以查看真正被抛出的异常类型。 Then handle those cases. 然后处理这些案件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM