如何在XML子节点中添加多个属性及其对应的值

huangapple go评论97阅读模式
英文:

How to add multiple attribute and corresponding values within xml child node

问题

要向XML子节点添加多个属性和值。

例如,我想在Namespace元素内添加另一个Rule。

<GlobalAppSettings>
  <NameSpace name="retail">
  </NameSpace>
  <NameSpace name="APEService" inheritsFrom="retail">
    <Rules>
      <Rule namespace="retail" queues="Default" scanInterval="10000" threads="2" />
      <Rule namespace="retail" queues="DLS" scanInterval="9000" threads="1" />
      <!-- 在这里添加另一个Rule -->
    </Rules>
  </NameSpace>
</GlobalAppSettings>

我尝试添加另一个元素和文本节点,但卡在这一点上,无法继续。

英文:

Add multiple attributes and values to a xml child node.
Ex- I want to add another Rule within Namespace element.

&lt;GlobalAppSettings 
  &lt;NameSpace name=&quot;retail&quot; 
  &lt;/NameSpace&gt;
  &lt;NameSpace name=&quot;APEService&quot; inheritsFrom=&quot;retail&quot;&gt;
	&lt;Rules&gt;
	  &lt;Rule namespace=&quot;retail&quot; queues=&quot;Default&quot; scanInterval=&quot;10000&quot; threads=&quot;2&quot; /&gt;
	  &lt;Rule namespace=&quot;retail&quot; queues=&quot;DLS&quot; scanInterval=&quot;9000&quot; threads=&quot;1&quot; /&gt;
	 
	&lt;/Rules&gt;
  &lt;/NameSpace&gt;
&lt;/GlobalAppSettings&gt;

I tried adding another element and a text node but am stuck at this point and unable to proceed.

答案1

得分: 1

您可以使用System.Xml.XmlDocument来完成这个操作。

以下是如何向您提到的 XML 添加额外规则的示例:

$pathToXml = "C:\temp\xml.xml"

$xml = New-Object System.Xml.XmlDocument
$xml.Load($pathToXml)
$namespaceNode = $xml.SelectSingleNode("//NameSpace[@name='APEService']")
$rulesNode = $namespaceNode.SelectSingleNode("Rules")

# 添加新规则:
$ruleNode = $xml.CreateElement("Rule")
$ruleNode.SetAttribute("namespace", "retail")
$ruleNode.SetAttribute("queues", "sometimes")
$ruleNode.SetAttribute("scanInterval", "8000")
$ruleNode.SetAttribute("threads", "2")
$rulesNode.AppendChild($ruleNode)

# 移除规则:
# 通过队列名选择要移除的规则
# 如果需要移除整个命名空间,可以使用@namespace而不是@queues
$ruleToRemove = $rulesNode.SelectSingleNode("Rule[@queues='DLS']")

# 从父节点中移除规则
$rulesNode.RemoveChild($ruleToRemove)

$xml.Save($pathToXml)
英文:

You can use the
System.Xml.XmlDocument
to accomplice this

here is an example of how to add an extra rule to your mentioned xml

$pathToXml = &quot;C:\temp\xml.xml&quot;

$xml = New-Object System.Xml.XmlDocument
$xml.Load($pathToXml)
$namespaceNode = $xml.SelectSingleNode(&quot;//NameSpace[@name=&#39;APEService&#39;]&quot;)
$rulesNode = $namespaceNode.SelectSingleNode(&quot;Rules&quot;)

# Adding a new rule:
$ruleNode = $xml.CreateElement(&quot;Rule&quot;)
$ruleNode.SetAttribute(&quot;namespace&quot;, &quot;retail&quot;)
$ruleNode.SetAttribute(&quot;queues&quot;, &quot;sometimes&quot;)
$ruleNode.SetAttribute(&quot;scanInterval&quot;, &quot;8000&quot;)
$ruleNode.SetAttribute(&quot;threads&quot;, &quot;2&quot;)
$rulesNode.AppendChild($ruleNode)

# Removing a rule:
# Select the rule to be removed by the queues name 
# if you need to remove a complete namespace you could just use @namespace instead of @queues
$ruleToRemove = $rulesNode.SelectSingleNode(&quot;Rule[@queues=&#39;DLS&#39;]&quot;)

# Remove the rule from the parent node
$rulesNode.RemoveChild($ruleToRemove)

$xml.Save($pathToXml)

huangapple
  • 本文由 发表于 2023年3月9日 23:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定