XML 文件中缺少 XML 元素
我正在尝试从不同的机器读取 XML 文件,其中一些文件可能具有其他文件没有的数据元素。目前,我正在使用 Try-Catch 块来处理这些情况,但我想知道是否有更好的方法来做到这一点,有什么想法吗?
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 { }
}
好的,我根据 John Saunders 的帖子找到了一个解决方案:
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);
}
}
我同意空的 Catch 块,但为了测试目的我将它们删除了。此后,我编辑了我的帖子,了解我的 Try-Catch 块现在的样子。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是“处理”问题的可怕方式。如果 XML 可能确实缺少元素,则在尝试访问这些元素之前检查这些元素是否存在。
您的空 catch 块会忽略其中发生的所有异常,而不仅仅是意味着元素丢失的异常。
加载表后,列就会显示或不显示。您可以使用 ds.Tables[config.GlobalVars.Paths].Columns.Contains("columnName") 来确定该列是否存在。
如果存在列,则对于任何给定行,该列可能为空,也可能不为空。使用
drv.Row.IsNull("columnName")
确定该行中的该列是否为空。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.好的,我刚刚注意到 XmlDataDocument 很快就会被弃用,所以我决定删除它并使用 Linq to XML,这是我的新解决方案
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