Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XDocument.Load(XmlReader) Possible Exceptions

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.

like image 574
Joe DePung Avatar asked Aug 01 '11 21:08

Joe DePung


2 Answers

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.

like image 145
Nastya Kholodova Avatar answered Nov 18 '22 10:11

Nastya Kholodova


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
}
like image 26
ˈvɔlə Avatar answered Nov 18 '22 12:11

ˈvɔlə