XDocument.Load(XmlReader) Possible Exceptions

2019-04-18 14:39发布

问题:

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.

Thanks in advance for your assistance.

回答1:

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 and SecurityException

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

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

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.



回答2:

XmlReader.Create(Stream) allows two types of exceptions: [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
}


回答3:

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. 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. 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. Then handle those cases.