如何检查GetElementsByTagName是否不匹配

时间:2013-11-04 10:07:57

标签: c# xml

我有一个脚本,可以使用InnerText

将xml加载/读取到15个texbox

这是代码。

doc = new XmlDocument();
doc.Load(dlgOpenFile.FileName);
root = doc.DocumentElement;
txt1.Text = root.GetElementsByTagName("Seksi")[0].InnerText;
txt2.Text = root.GetElementsByTagName("Kota")[0].InnerText;
txt3.Text = root.GetElementsByTagName("Tanggal")[0].InnerText;
txt4.Text = root.GetElementsByTagName("NoIntelejen")[0].InnerText;
txt5.Text = root.GetElementsByTagName("Peta")[0].InnerText;
txt6.Text = root.GetElementsByTagName("Kedar")[0].InnerText;
txt7.Text = root.GetElementsByTagName("Tahun")[0].InnerText;
txt8.Text = root.GetElementsByTagName("Lembar")[0].InnerText;
txt9.Text = root.GetElementsByTagName("TugasPokok")[0].InnerText;
txt10.Text = root.GetElementsByTagName("Intelejen")[0].InnerText;
txt11.Text = root.GetElementsByTagName("Taktis")[0].InnerText;
txt12.Text = root.GetElementsByTagName("Personil")[0].InnerText;
txt13.Text = root.GetElementsByTagName("Logistik")[0].InnerText;
txt14.Text = root.GetElementsByTagName("Teritorial")[0].InnerText;
txt15.Text = root.GetElementsByTagName("Perhubungan")[0].InnerText;

当我加载正确的xml时,xml成功加载到文本框,但是当GetElementsByTagName不匹配时,显示错误“对象引用未设置为对象的实例。”

在行

txt10.Text = root.GetElementsByTagName("Intelejen")[0].InnerText;

如何检查GetElementsByTagName是否不匹配,所以当元素不匹配时,应用会显示消息并取消加载?

6 个答案:

答案 0 :(得分:3)

GetElementsByTagName方法将返回所有匹配元素的节点集合,如果未找到匹配项,则它将返回空集合。您可以使用Count属性测试集合是否为空。这就是你需要的:

var matches = root.GetElementsByTagName("Seksi");
if(matches.Count > 0)
    txt1.Text = matches[0].InnerText;

正如您多次这样做,创建辅助函数会很有用。像这样:

public void SetTextForTag(XmlElement root, TextBox tb, string tag)
{
    var matches = root.GetElementsByTagName(tag);
    if(matches.Count > 0)
        tb.Text = matches[0].InnerText;
}

你可以像这样使用,例如:

SetTextForTag(root, txt1, "Seksi");
SetTextForTag(root, txt2, "Kota");
//and so on...

答案 1 :(得分:0)

为什么不使用三元运算符,如:

   txt1.Text = root.GetElementsByTagName("Seksi").Count < 1 ? "" : root.GetElementsByTagName("Seksi")[0].InnerText;

答案 2 :(得分:0)

您可以使用此扩展方法来干扰您的代码

public static bool TryGetInnerText(this XmlElement root, string childName, out string text)
    {
         var children = root.GetElementsByTagName(childName);
         if(children.Count > 0){
               text = children[0].InnerText;
               return true;
         }
         text = null;
         return false;
    }

然后你可以这样称呼它:

 if(!root.TryGetInnerText("Seksi", out txt1.Text)){
      //notify the uesr that the Seksi wasn't found
 }      

这也将为您处理支票

答案 3 :(得分:-1)

试试这个:

var element = root.GetElementsByTagName("Intelejen")[0];
if (element != null)
    txt10.Text = element.InnerText

说明:如果未找到任何元素,root.GetElementsByTagName("Intelejen")[0]将返回null。这就是InnerText抛出空异常

的原因

答案 4 :(得分:-2)

对于不存在的元素,请记住空值检查,不能指定不存在的元素:

txt1.Text = (root.GetElementsByTagName("Seksi")[0] != null) 
   ? root.GetElementsByTagName("Seksi")[0].InnerText 
   : String.Empty;

答案 5 :(得分:-2)

var elements = root.Elements("Seksi");
        if (elements.Any())
        {
            string txt = elements.First().Value;
        }