Missing XML Elements in an XML file
I am trying to read XML files from different machines, some of these files may have data elements that others don't have. Currently, I am using a Try-Catch block to handle these situations but I was wondering if there was a better way of doing this, any thoughts?
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("MachineData.xml");
DataSet ds = new DataSet("AppData");
ds = xmlDatadoc.DataSet;
DataView dv = new DataView(ds.Tables[config.GlobalVars.Paths]);
foreach (DataRowView drv in dv)
{
try
{
cApp.TransferProtcol = drv["TransferProtocol"].ToString();
}
catch 开发者_运维百科{ }
try
{
cApp.RemoteServerPath = drv["RemoteServer"].ToString();
}
catch { }
}
Ok, I figured out a solution based upon John Saunders post:
if(ds.Tables[0].Columns.Contains("TransferProtocol")
{
try
{
if (drv["TransferProtocol"].ToString().Length > 0)
{
cApp.TransferProtcol = drv["TransferProtocol"].ToString();
}
}
catch(Exception e)
{
Messagebox.Show(e.Message);
}
}
I agree about the empty Catch blocks but I stubbed them out for testing purposes. I have since edited my post as to what my Try-Catch block looks now.
That's a horrible way to "handle" the problem. If the XML may legitimately be missing elements, then check to see if the elements are present before trying to access them.
Your empty catch blocks ignore all exceptions that occur inside of them, not just exceptions that mean the element is missing.
The columns are present or not as soon as the table is loaded. You can use ds.Tables[config.GlobalVars.Paths].Columns.Contains("columnName")
to determine whether or not the column exists.
If a column exists, then for any given row, the column may or may not be null. Use drv.Row.IsNull("columnName")
to determine whether that column in that row is null.
Ok, I just noticed that XmlDataDocument is going to be deprecated soon, so I decided to scrape it and use Linq to XML, and here is my new sopultion
public List<cApplication> GetAppSettings()
{
if (!File.Exists(Config.System.XMLFilePath))
{
WriteXMLFile();
}
try
{
XDocument data = XDocument.Load(Config.System.XMLFilePath);
return (from c in data.Descendants("Application")
orderby c.Attribute("Name")
select new cApplication()
{
LocalVersion = (c!=null)?c.Element("Version").Value:string.Empty,
RemoteVersion = (c!=null)?c.Element("RemoteVersion").Value:string.Empty,
DisableApp = (c!=null)?((YesNo)Enum.Parse(typeof(YesNo), c.Element("DisableApplication").Value, true)):YesNo.No,
SuspressMessages = (c != null) ? ((YesNo)Enum.Parse(typeof(YesNo), c.Element("SuspressMessage").Value, true)):YesNo.No
}).ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
List<cApplication> l = new List<cApplication>().ToList();
return l.ToList();
}
}
精彩评论