2013-11-04 25 views
0

我有一个脚本加载/读取XML到15个texboxes用的innerText如何检查的getElementsByTagName不匹配

这里是代码。

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不匹配,所以当元素不匹配,应用程序显示消息并取消负荷?

+1

你似乎总是利用'GetElementsByTagName'收集的'First'值...为什么不使用'SelectSingleNode'? – Arran

回答

-1

试试这个:

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

说明:root.GetElementsByTagName("Intelejen")[0],则返回null元素没有被发现。这就是为什么InnerText引发空异常记

+0

为什么我的答案downvoted?在空的XmlElementList上使用索引操作符确实返回null,而不像其他的List不会抛出IndexOutOfRange异常。 – Kabbalah

+0

不知道为什么这是downvoted,我想也许downvoter不明白你有什么解释。我不愿意为upvote反对downvote,对不起 – musefan

+0

感谢您的重播,因为我在考虑我的代码段错误。 – Kabbalah

-2

保持零检查不存在的元素,你不能指定哪些是不存在的:

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

关心精心设计,downvoters? –

+0

这将导致相同的错误,因为它不是'InnerText',它是null。它是不存在的'[0]'元素是空的 – musefan

+0

@musefan够了;-)编辑 –

2

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... 
+0

我试过你的代码,没有错误时不是元素不匹配,但我想,当用户加载不匹配元素,应用程序显示消息框并取消加载。 @musefan –

0

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

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

调用'GetElementsByTagName'两次效率不高 – musefan

+0

但它不会浪费更多的代码行。 – Edper

+0

它有多少行代码并不重要,这对编译的程序没有任何意义。调用该函数两次意味着它必须运行两次,这可能是一个昂贵的大文件函数 – musefan

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

函数'XmlElement'中不存在函数'Elements',所以这甚至不会编译 – musefan

+0

@musefan:您在使用XDocument时有任何限制吗? – PKV

+0

这不是我的问题,所以我不能指定要求,但是您的答案中甚至没有提及XDocument,因此OP如何知道它是您的解决方案的一项要求? – musefan

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 
}  

,并且将处理检查你还有