我如何才能找到我的XML特定的节点? [英] How can I find a specific node in my XML?
本文介绍了我如何才能找到我的XML特定的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从以下XML读取XML节点名,但我不知道该怎么做。
下面是XML:
<?XML版本=1.0独立=YES>?;
<游戏和GT;
<游戏>
<名称>谷歌吃豆< /名称>
< URL> HTTP:\\www.google.de< / URL>
< /游戏>
< /游戏>
代码:
使用的System.Xml;
命名空间SRCDSGUI
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
}
私人无效Form1_Load的(对象发件人,EventArgs五)
{
XmlDocument的DOC =新的XmlDocument();
doc.Load(Application.StartupPath + @\games.xml);
的XmlElement根= doc.DocumentElement;
XmlNodeList中的节点= root.SelectNodes(//游戏);
的foreach(在节点XmlNode的节点)
{
listBox1.Items.Add(节点[游戏]的InnerText。);
}
}
}
}
解决方案
可能尝试这种
XmlNodeList中的节点=根。的SelectNodes(//游戏/游戏)
的foreach(在节点XmlNode的节点)
{
listBox1.Items.Add(节点[名称]的InnerText。);
}
I have to read the xml node "name" from the following XML, but I don't know how to do it.
Here is the XML:
<?xml version="1.0" standalone="yes" ?>
<games>
<game>
<name>Google Pacman</name>
<url>http:\\www.google.de</url>
</game>
</games>
Code:
using System.Xml;
namespace SRCDSGUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\games.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//games");
foreach (XmlNode node in nodes)
{
listBox1.Items.Add(node["game"].InnerText);
}
}
}
}
解决方案
May be try this
XmlNodeList nodes = root.SelectNodes("//games/game")
foreach (XmlNode node in nodes)
{
listBox1.Items.Add(node["name"].InnerText);
}
这篇关于我如何才能找到我的XML特定的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文