gpt4 book ai didi

c# - 更新 xml 文件中的值

转载 作者:可可西里 更新时间:2023-11-01 09:10:40 24 4
gpt4 key购买 nike

我有一个 xml 文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
<level>
<node1 />
<node2 />
<node3 />
</level>
</root>

在 node1、node2、node3 中插入​​值的最简单方法是什么?

C#、Visual Studio 2005

最佳答案

给你:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(@"
<root>
<level>
<node1 />
<node2 />
<node3 />
</level>
</root>");
XmlElement node1 = xmldoc.SelectSingleNode("/root/level/node1") as XmlElement;
if (node1 != null)
{
node1.InnerText = "something"; // if you want a text
node1.SetAttribute("attr", "value"); // if you want an attribute
node1.AppendChild(xmldoc.CreateElement("subnode1")); // if you want a subnode
}

关于c# - 更新 xml 文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2137957/

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