简体   繁体   English

如何在我的 XML 中找到特定节点?

[英]How can I find a specific node in my XML?

I have to read the xml node "name" from the following XML, but I don't know how to do it.我必须从以下XML读取xml节点“名称”,但我不知道该怎么做。

Here is the XML:这是 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);
            }


        }
    }
}

Maybe try this也许试试这个

XmlNodeList nodes = root.SelectNodes("//games/game")
foreach (XmlNode node in nodes)
{
    listBox1.Items.Add(node["name"].InnerText);
}

Or try this:或者试试这个:

XmlNodeList nodes = root.GetElementsByTagName("name");
for(int i=0; i<nodes.Count; i++)
{
listBox1.Items.Add(nodes[i].InnerXml);
}

You are really close - you found the game node, why don't you go a step further and just get the name node if it exists as a child under game?你真的很接近 - 你找到了游戏节点,你为什么不更进一步 go 如果它作为游戏中的孩子存在,就得到名称节点?

in your for each loop:在你的每个循环中:

listBox1.Items.Add(node.SelectSingleNode("game/name").InnerText);

Here is an example of simple function that finds and fetches two particular nodes from XML file and returns them as string array这是一个简单的 function 示例,它从 XML 文件中查找并获取两个特定节点,并将它们作为字符串数组返回

private static string[] ReadSettings(string settingsFile)
    {
        string[] a = new string[2];
        try
        {
            XmlTextReader xmlReader = new XmlTextReader(settingsFile);
            while (xmlReader.Read())
            {
                switch (xmlReader.Name)
                {
                    case "system":
                        break;
                    case "login":
                        a[0] = xmlReader.ReadString();
                        break;
                    case "password":
                        a[1] = xmlReader.ReadString();
                        break;
                }

            }    
            return a;
        }
        catch (Exception ex)
        {
            return a;
        }
    }
import xml.etree.ElementTree as ET

tree= ET.parse('name.xml')
root= tree.getroot()

print root[0][0].text
  • root = games根=游戏
  • root[0] = game root[0] = 游戏
  • root[0][0] = name根[0][0] = 名称
  • root[0][1] = url根[0][1] = url
  • use the ".text" to get string representation of value使用“.text”获取值的字符串表示
  • this example is using python此示例使用 python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM