将数据附加到XML文件 [英] Appending data to an XML file
本文介绍了将数据附加到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在研究xml结构,并且在添加到XML文件的特定部分时需要帮助。我的目标是在类别节点下添加新类别。下面是我目前的代码。
I have been working on an xml structure and Im having issues adding to a specifc part of the XML file and need help. My goal is to add new category under the Categories node. below is my current code.
$Path = "C:\Users\shuppz\Desktop\XML\Helpbox.xml"
$xmlDoc = [System.Xml.XmlDocument](Get-Content $Path)
#Create New Category
$newCat = $xmlDoc.AdminLauncher.Categories.AppendChild($xmlDoc.CreateElement("Category"))
#Set Attributes
$newCat.SetAttribute("Selected", "False")
$newCat.SetAttribute("Name", $Name)
#Save File
$xmlDoc.Save($Path)
我的XML:
My XML:
<?xml version="1.0"?>
<!--XML For Admin Launcher-->
<AdminLauncher>
<Categories />
<Task Name="GPMC" Category="Tools">
<Path>C:\Windows</Path>
<Arg>/offerra</Arg>
</Task>
</AdminLauncher>
当我运行它时,我得到了以下错误:
When I run it I get the below errors:
Method invocation failed because [System.String] doesn't contain a method named 'AppendChild'.
At C:\Users\shuppz\AppData\Local\Temp\5564e900-f0cf-4878-8c39-9fc3aff6ed8e.ps1:6 char:55
+ $newCat = $xmlDoc.AdminLauncher.Categories.AppendChild <<<< ($xmlDoc.CreateElement("Category"))
+ CategoryInfo : InvalidOperation: (AppendChild:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
You cannot call a method on a null-valued expression.
At C:\Users\shuppz\AppData\Local\Temp\5564e900-f0cf-4878-8c39-9fc3aff6ed8e.ps1:9 char:21
+ $newCat.SetAttribute <<<< ("Selected", "False")
+ CategoryInfo : InvalidOperation: (SetAttribute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\shuppz\AppData\Local\Temp\5564e900-f0cf-4878-8c39-9fc3aff6ed8e.ps1:10 char:21
+ $newCat.SetAttribute <<<< ("Name", $Name)
+ CategoryInfo : InvalidOperation: (SetAttribute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
推荐答案
Path =C:\ Users \shuppz \Desktop\XML \Helpbox.xml
Path = "C:\Users\shuppz\Desktop\XML\Helpbox.xml"
xmlDoc = [System.Xml.XmlDocument ](获取内容
xmlDoc = [System.Xml.XmlDocument](Get-Content
路径)
#Create新类别
Path) #Create New Category
这篇关于将数据附加到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文