gpt4 book ai didi

c# - 如何使用 C# 替换特定的 xml 节点元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:20 25 4
gpt4 key购买 nike

给定本地xml文件:

<products>
<product id="p1">
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</product>
<product id="p2">
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</product>
<product id="p3">
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</product>
<product id="p4">
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</product>
<product id="p5">
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</product>
</products>

我已尝试按如下方式替换产品节点“p1”中的价格元素:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System;
using System.Xml.XPath;
using System.Xml.Linq;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"products.xml");

Console.WriteLine("\n\nDisplay the initial XML...");
xmlDoc.Save(Console.Out);

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("products", "product");

// replace the node with a new one
//Select the profile node with the matching attribute value.
XmlNode product;
XmlElement root = xmlDoc.DocumentElement;
product = root.SelectSingleNode("descendant::product[id='p1']", nsmgr);

//Create a new price element.
XmlElement oldElem = xmlDoc.CreateElement("price");
oldElem.InnerText = "800";

//Create a new price element.
XmlElement newElem = xmlDoc.CreateElement("price");
newElem.InnerText = "125";

//Replace the price element.
root.ReplaceChild(newElem, root.FirstChild);
Console.WriteLine("\n\nDisplay the modified XML...");
xmlDoc.Save(Console.Out);

// save the document with the revised node
xmlDoc.Save(@"products2.xml");

问题是新节点(价格)元素只是添加到产品 p1 节点,当保存到磁盘时,它会丢弃所有 p1。我做错了什么?

最佳答案

主要问题在

product = root.SelectSingleNode("descendant::product[id='p1']", nsmgr);

因为您没有在下面使用变量。

下一期在 [id='p1'] ,因为您像访问元素一样访问 ID,但它应该是一个属性。使用 [@id='p1']相反。

其他:

  • 可能会更新 <price> 的内部文本直接节点而不是替换整个元素。
  • 不需要命名空间管理器,因为您的示例中没有命名空间。
  • 无需创建节点oldNode .该节点已存在。
  • newElem未使用。

建议的风格修正:

using System;
using System.Xml;

namespace XmlUpdateNode
{
class Program
{
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"products.xml");

Console.WriteLine("\n\nDisplay the initial XML...");
xmlDoc.Save(Console.Out);

// replace the node with a new one
//Select the profile node with the matching attribute value.
var product = xmlDoc.SelectSingleNode("descendant::product[@id='p1']");

//Create a new price element.
XmlElement elem = xmlDoc.CreateElement("price");
elem.InnerText = "125";

//Replace the price element.
product.ReplaceChild(elem, product.FirstChild.NextSibling);
Console.WriteLine("\n\nDisplay the modified XML...");
xmlDoc.Save(Console.Out);

// save the document with the revised node
xmlDoc.Save(@"products2.xml");
}
}
}

通过直接替换 price 元素内的文本甚至更短:

using System;
using System.Xml;

namespace XmlUpdateNode
{
class Program
{
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"products.xml");

Console.WriteLine("\n\nDisplay the initial XML...");
xmlDoc.Save(Console.Out);

// replace the price directly
var product = xmlDoc.SelectSingleNode("descendant::product[@id='p1']/price");
product.InnerText = "125";

Console.WriteLine("\n\nDisplay the modified XML...");
xmlDoc.Save(Console.Out);

// save the document with the revised node
xmlDoc.Save(@"products2.xml");
}
}
}

关于c# - 如何使用 C# 替换特定的 xml 节点元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38338509/

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