如何使用PowerShell将新元素附加到XML。

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

How to appen new elements into XML using powershell

问题

我需要在XML文件中创建新的标签,然后用新的元素填充该新标签。我将在一个脚本中多次执行此操作。我能够创建新的标签,但当我尝试用新元素填充它时出现错误。

  1. $file = "文件路径"
  2. [System.Xml.XmlDocument]$xmlObject = New-Object System.Xml.XmlDocument
  3. $xmlObject = [System.Xml.XmlDocument](Get-Content $file)
  4. #首先添加<authorization>标签
  5. $newXmlElement = $xmlObject.CreateElement('authorization')
  6. $newXmlElement.InnerText = ""
  7. $xmlObject.configuration.AppendChild($newXmlElement)
  8. #然后尝试用更多数据填充标签
  9. $newXmlElement = $xmlObject.CreateElement('deny')
  10. $newXmlElement.SetAttribute('users', '?')
  11. $xmlObject.configuration.authorization.AppendChild($newXmlElement)

当我尝试在代码的最后一行调用新标签时,出现以下错误:
由于[System.String]不包含名为'AppendChild'的方法,因此方法调用失败。

我该如何填充新标签以及期望的结果:

  1. <configuration>
  2. <authorization>
  3. <deny users="?" />
  4. </authorization>
  5. </configuration>
英文:

I need to create new tag within XML file and then fill the new tag with new elements. I will do this multiple times in one script. I am able to create the new tag but when i try to fill it with new elements i get an error.

My code so far:

  1. $file = &quot;filepath&quot;
  2. [System.Xml.XmlDocument]$xmlObject = New-Object System.Xml.XmlDocument
  3. $xmlObject = [System.Xml.XmlDocument](Get-Content $file)
  4. #first i add the &lt;authorization&gt; tag
  5. $newXmlElement = $xmlObject.CreateElement(&#39;authorization&#39;)
  6. $newXmlElement.InnerText = &quot;&quot;
  7. $xmlObject.configuration.AppendChild($newXmlElement)
  8. #then i try to fill the tag with more data
  9. $newXmlElement = $xmlObject.CreateElement(&#39;deny&#39;)
  10. $newXmlElement.SetAttribute(&#39;users&#39;, &#39;?&#39;)
  11. $xmlObject.configuration.authorization.AppendChild($newXmlElement)

When i try to call the new tag in last line of code i get this error:
Method invocation failed because [System.String] does not contain a method named 'AppendChild'

How would i go about filling the new tag with new element?
Desired result:

  1. &lt;configuration&gt;
  2. &lt;authorization&gt;
  3. &lt;deny users=&quot;?&quot; /&gt;
  4. &lt;/authorization&gt;
  5. &lt;/configuration&gt;

答案1

得分: 1

我会在这种情况下使用xpath和“片段”:

  1. $newelem=@&#39;
  2. &lt;authorization&gt;
  3. &lt;deny users=&quot;?&quot; /&gt;
  4. &lt;/authorization&gt;
  5. &#39;@
  6. $dest = $xmlObject.SelectSingleNode(&#39;//configuration&#39;)
  7. $xmlFragment=$xmlObject.CreateDocumentFragment()
  8. $xmlFragment.InnerXML=$newelem
  9. $dest.AppendChild($xmlFragment)
英文:

I would use xpath and fragment in this case:

  1. $newelem=@&#39;
  2. &lt;authorization&gt;
  3. &lt;deny users=&quot;?&quot; /&gt;
  4. &lt;/authorization&gt;
  5. &#39;@
  6. $dest = $xmlObject.SelectSingleNode(&#39;//configuration&#39;)
  7. $xmlFragment=$xmlObject.CreateDocumentFragment()
  8. $xmlFragment.InnerXML=$newelem
  9. $dest.AppendChild($xmlFragment)

This works for your simplified xml in the question and you may have to modify it to fit your actual xml file.

huangapple
  • 本文由 发表于 2023年4月20日 04:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058693.html
匿名

发表评论

匿名网友

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

确定