XML读取子节点 [英] XML reading child nodes
本文介绍了XML读取子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML文档,我正在尝试获取名为 Unit
I have an XML document and I am trying to get the childnodes of an element called Unit
我的XML文档的布局如下:
My XML Doc is layed out like so:
<Unit>
<id>3</id>
<name>System Information</name>
<description>null</description>
... other ...
</Unit>
这是我用来尝试阅读它们的代码.
This is the code I am using to try and read them.
public void Load()
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnode;
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagName("Units");
for (int i = 0; i < xmlnode.Count; i++)
{
string str = string.Format("ID: {0}\r\nName:{0}", xmlnode[i].ChildNodes.Item(0).InnerText, xmlnode[i].ChildNodes.Item(1).InnerText);
MessageBox.Show(str);
}
}
但是问题是,当我尝试获取它们时,它不会显示项目0或项目1,而是显示所有项目,而不是我选择的项目.
But the problem is, when I try and get them, instead of getting item 0 or item 1, it displays all the items and not the items i have selected.
推荐答案
正如我从您的xml和代码中看到的那样.您在行中有错误:
As I can see from your xml and code. You have error in line:
xmlnode = xmldoc.GetElementsByTagName("Units");
将其更改为:
xmlnode = xmldoc.GetElementsByTagName("Unit");
这篇关于XML读取子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文