I'm working on some code that loads an xml file at run time. At the minute, we're using the XmlDocument type to read the xml file and wrapping a try-catch around SelectSingleNode statement (this is done on the off chance that a node is null, or isn't there as we're parsing user created xml files).
Please note: I realise that XmlDocument has been replaced by XDocument. However since we're working with .NET version 3 (according to this MSDN document XDocument isn't available in .NET 3), we're having to stick with XmlDocument for now. We're using .NET 3 for a variety of reasons (some of which are spec related).
Here's an example of what we're doing at the minute:
private void LoadUserXMLFile ()
{
XmlDocument xDoc = new XmlDocument();
XmlTextReader reader = new XmlTextReader(fileName);
reader.Read();
xDoc.Load(reader);
try { firstElementString = xDoc.SelectSingleNode(<path to node>).InnderText);
catch { <exception handling here > }
//more SelectSingleNode statements, each wrapped inside
//individual try-catch blocks
}
Obviously the above is an example, and I've simplified the catch statement.
I've written a schema for the user generated XML files that this app will work with, and I was wondering if I used the schema (in some way) during parsing of an XML document, would I still need to wrap each SelectSingleNode with try-catch statements?
Is it even possible to use the schema (in some way) during parsing to check the XML document has the correct format and all of the required elements?