Joe DePung
Joe DePung

Reputation: 1031

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.

Upvotes: 18

Views: 18005

Answers (4)

Daniel Jonsson
Daniel Jonsson

Reputation: 3931

I was following a tutorial about web scraping in .NET 6 using XDocument. However, when I called XDocument.Load("...") to load an .html page, I got the following exception:

System.Xml.XmlException: ''src' is an unexpected token. The expected token is '='. Line 47, position 32.'

So depending on what you will parse, and if you need something more permissive, perhaps another library is more suited. I went with IronWebScraper, which works better for the HTML I am working with.

Upvotes: 0

ˈvɔlə
ˈvɔlə

Reputation: 10272

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
}

Upvotes: 3

Nastya Kholodova
Nastya Kholodova

Reputation: 1321

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.

Upvotes: 22

C.J.
C.J.

Reputation: 16111

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.

Upvotes: 0

Related Questions