gpt4 book ai didi

c# - 遍历 XML 文档

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:55 26 4
gpt4 key购买 nike

我的方法:


if (File.Exists( @"C:\config.xml"))
{
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load( @"C:\config.xml");
System.Xml.XmlElement root = xd.DocumentElement;
System.Xml.XmlNodeList nl = root.SelectNodes("/config");
foreach (System.Xml.XmlNode xnode in nl)
{
string name = xnode.Name;
string value = xnode.InnerText;
string nv = name + "|" + value;
Send(nv);
}

我的 Xml 文档

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<config>
<bla>D</bla>
<def>300</def>
<ttOUT>34000</ttOUT>
<num>3800</num>
<pw>help</pw>
<err>1</err>
....and so on
</config>

现在我的方法只返回前 2 个。我做错了什么......

最佳答案

使用 System.Xml 命名空间来避免长类型限定,即...

    using System.Xml;

然后尝试这样的事情..

    XmlNodeList nl = xd.SelectNodes("config");
XmlNode root = nl[0];

foreach (XmlNode xnode in root.ChildNodes)
{
string name = xnode.Name;
string value = xnode.InnerText;
string nv = name + "|" + value;
Send(nv);
}

我认为您的方法有问题。

a) 我不认为 SelectNodes 应该采用 /config 参数,而应该采用 config

b) 选择第一个(也是唯一一个 - .Net 中的 XML 文件必须有一个且只有一个根节点)根节点后,您需要遍历根的 ChildNodes

关于c# - 遍历 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4757122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com