XDocument.Load(XmlReader中)可能的例外 [英] XDocument.Load(XmlReader) Possible Exceptions

查看:177
本文介绍了XDocument.Load(XmlReader中)可能的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是可以在 XDocument.Load(的XmlReader)被称为抛出的可能例外?事先很难遵循最佳实践(即避免通用的try catch块)时,文档未能提供关键信息。

What are the possible exceptions that can be thrown when XDocument.Load(XmlReader) is called? It is hard to follow best practices (i.e. avoiding generic try catch blocks) when the documentation fails to provide crucial information.

感谢您的帮助。

推荐答案

MSDN说:LINQ到XML的加载功能在XmlReader.Therefore建成后,你可能赶上了由XmlReader中引发的任何异常。创建超载方法和读取和解析文档的XmlReader方法。

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. Create overload methods and the XmlReader methods that read and parse the document.

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

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

编辑:MSDN并不总是说真的。所以我分析Load方法代码反射,得到了这样的结果:

MSDN not always says true. So I've analyzed Load method code with reflector and got results like this:

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



方法Load是调用方法:

Method Load is calling method:

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

我们可以得到下一个例外:ArgumentNullException,XmlException和InvalidOperationException异常。
MSDN说,你可以得到SecurityException异常,但也许你可以得到这种类型的异常,而创建的XmlReader。

We could get the next exceptions:ArgumentNullException, XmlException and InvalidOperationException. MSDN says that you could get SecurityException, but perhaps you can get this type of exception while creating XmlReader.

这篇关于XDocument.Load(XmlReader中)可能的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆