You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#读取XML节点值异常:content节点循环仅执行一次

问题:XML读取中content节点下的循环仅执行一次的解决方案

我明白你碰到的棘手问题了——明明XML里content下有3个idarticle节点,循环却只跑一次,这其实是你写的XPath选对了范围,咱们一步步捋清楚:

问题根源

你当前获取xnInhalt的代码用了这个XPath:

XmlNodeList xnInhalt = xml.SelectNodes("/lagerverwaltung/article/orders/order[@id='" + id + "']/content");

这个路径选中的是整个content节点本身(XML里只有这一个content节点),而不是它下面的3个idarticle子节点。所以你的foreach循环其实是在遍历这一个content节点,自然只会执行一次,而且每次只能拿到第一个idarticle的值,另外两个直接被漏掉了。

另外还有个小瑕疵:用全局的xml.SelectNodes加拼接id的方式查询,不仅麻烦,还可能因为id的特殊字符出bug,其实直接基于当前的xn(也就是当前遍历的order节点)做相对查询,会更高效也更安全。

修正后的代码

这里给你两种可行的修正方案,更推荐第二种:

方式一:调整XPath直接选中idarticle节点

把XPath改成指向content下的所有idarticle:

List<Order> orderList = new List<Order>();
XmlDocument xml = new XmlDocument();
xml.Load(xmlFilePath);
XmlNodeList xnList = xml.SelectNodes("/lagerverwaltung/article/orders/order");
foreach (XmlNode xn in xnList)
{
    // 这部分保留你原有代码,正常工作
    string id = xn["id"].InnerText;
    string bestellPositionId = xn["idposition"].InnerText;
    string kundeId = xn["idcustomer"].InnerText;
    string bestelldatum = xn["orderdate"].InnerText;
    string rechnungsDatum = xn["paydate"].InnerText;
    string mwst = xn["mwst"].InnerText;
    string rechnungsTotal = xn["total"].InnerText;

    // 修改XPath,选中content下的所有idarticle节点
    XmlNodeList xnInhalt = xml.SelectNodes("/lagerverwaltung/article/orders/order[@id='" + id + "']/content/idarticle");
    Dictionary<string, string> content= new Dictionary<string, string>();
    foreach (XmlNode xmlNode in xnInhalt) // 现在会循环三次
    {
        string articleid = xmlNode.InnerText;
        string amount = xmlNode.GetAttribute("amount");
        content.Add(articleid, amount);
    }

    // 记得把组装好的Order对象加入列表
    // orderList.Add(new Order { ..., Content = content });
}

方式二:基于当前order节点做相对查询(推荐)

不用全局查询,直接从当前遍历的xn节点出发查找,避免拼接id的麻烦:

List<Order> orderList = new List<Order>();
XmlDocument xml = new XmlDocument();
xml.Load(xmlFilePath);
XmlNodeList xnList = xml.SelectNodes("/lagerverwaltung/article/orders/order");
foreach (XmlNode xn in xnList)
{
    // 原有代码不变
    string id = xn["id"].InnerText;
    string bestellPositionId = xn["idposition"].InnerText;
    string kundeId = xn["idcustomer"].InnerText;
    string bestelldatum = xn["orderdate"].InnerText;
    string rechnungsDatum = xn["paydate"].InnerText;
    string mwst = xn["mwst"].InnerText;
    string rechnungsTotal = xn["total"].InnerText;

    // 用相对路径从当前order节点查询,简洁又安全
    XmlNodeList xnInhalt = xn.SelectNodes("./content/idarticle");
    Dictionary<string, string> content= new Dictionary<string, string>();
    foreach (XmlNode xmlNode in xnInhalt) // 循环三次,遍历所有idarticle
    {
        string articleid = xmlNode.InnerText;
        string amount = xmlNode.GetAttribute("amount");
        content.Add(articleid, amount);
    }

    // 将组装好的Order对象加入列表
    // orderList.Add(new Order { ... });
}

关键说明

  • 第二种方式里的./content/idarticle是相对路径,./表示从当前节点(也就是当前的order节点)开始查找,不仅代码更简洁,还避免了id拼接可能带来的错误(比如id包含特殊字符的情况)。
  • 现在循环的是每个idarticle节点,正好对应XML里的3个元素,循环会执行三次,完美符合你的预期。

内容的提问来源于stack exchange,提问作者User987123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:18:18