如果选择单个节点为空,则创建单个节点

发布于 2025-01-03 13:07:06 字数 838 浏览 1 评论 0原文

我有一个更新配置文件的程序。例如,配置文件可能包含:

<configuration>
  <userSettings>
    <setting name="phoneNumber" serializeAs="String">
      <value>123-456-7890</value>
    </setting>
  </userSettings>
</configuration>

要更新此配置文件,我使用以下命令:

XmlNode phoneNumberNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']");
phoneNumberNode.FirstChild.InnerText = this._cloudPublisherWebURL;

现在,在更新过程中我想更新电话号码和地址。地址可能存在于配置文件中,也可能不存在

如果 SelectSingleNode 为 null,我想创建一个具有给定路径的节点并设置其值。

XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='address']");
if(addressNode == null)
{
  //..Create the node here
}

如何在给定路径上创建具有值的节点?

I have a program that updates a config file. For example, the config file may contain:

<configuration>
  <userSettings>
    <setting name="phoneNumber" serializeAs="String">
      <value>123-456-7890</value>
    </setting>
  </userSettings>
</configuration>

To update this config file, I use the following:

XmlNode phoneNumberNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']");
phoneNumberNode.FirstChild.InnerText = this._cloudPublisherWebURL;

Now, during the update I want to update phoneNumber and address. Address may or may not be in the config file.

If SelectSingleNode is null, I would like to create a node with the given path and set its value.

XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='address']");
if(addressNode == null)
{
  //..Create the node here
}

How can I create the node with value at the given path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2025-01-10 13:07:06
XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings");
XmlNode setting = addressNode.Item(0).SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']");

setting.SetAttribute("name", "address"); //this is to change the name attribute value into address
XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings");
XmlNode setting = addressNode.Item(0).SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']");

setting.SetAttribute("name", "address"); //this is to change the name attribute value into address
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文