我有以下XML文件:
<?xml version="1.0" encoding="utf-8"?>
<!--This is my First XML File-->
<FileHeader>
<Item>
<AddendA Test="1"/>
<MICRDS Type="Regression"/>
<ImageViewDetail>
<ImageViewData Selected="Yes"/>
<ImageDS ArrivalDate="04022018"/>
<ImageViewAnalysis Status="Pending"/>
</ImageViewDetail>
<ImageViewDetail>
<ImageViewData Selected="Yes"/>
<ImageDS ArrivalDate="05022018"/>
<ImageViewAnalysis Status="Complete"/>
</ImageViewDetail>
<ImageViewDetail>
<ImageViewData Selected="No"/>
<ImageDS ArrivalDate="05022018"/>
<ImageViewAnalysis Status="In-Progress"/>
</ImageViewDetail>
</Item>
</FileHeader>
对于上述XML,我编写了以下PowerShell脚本:
$XML_Path = "D:\TEST_FOLDER\XML_FILES\Sample.xml"
# Create the XML File Tags
$Encoding = New-Object System.Text.UTF8Encoding($false)
$xmlWriter = New-Object System.XMl.XmlTextWriter($XML_Path, $Encoding)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteComment('This is my First XML File')
$xmlWriter.WriteStartElement('FileHeader')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
# Create the Initial Node
$CollectionNode = $xmlDoc.CreateElement("Item")
$xmlDoc.SelectSingleNode("//FileHeader").AppendChild($CollectionNode)
$CollectionNode = $xmlDoc.CreateElement("AddendA")
$xmlDoc.SelectSingleNode("//FileHeader//Item").AppendChild($CollectionNode)
$CollectionNode.SetAttribute("Test","1")
$CollectionNode = $xmlDoc.CreateElement("MICRDS")
$xmlDoc.SelectSingleNode("//FileHeader//Item").AppendChild($CollectionNode)
$CollectionNode.SetAttribute("Type","Regression")
$CollectionNode = $xmlDoc.CreateElement("ImageViewDetail")
$xmlDoc.SelectSingleNode("//FileHeader//Item").AppendChild($CollectionNode)
$CollectionNode = $xmlDoc.CreateElement("ImageViewData")
$xmlDoc.SelectSingleNode("//FileHeader//Item//ImageViewDetail").AppendChild($CollectionNode)
$CollectionNode.SetAttribute("Selected","Yes")
$CollectionNode = $xmlDoc.CreateElement("ImageDS")
$xmlDoc.SelectSingleNode("//FileHeader//Item//ImageViewDetail").AppendChild($CollectionNode)
$CollectionNode.SetAttribute("ArrivalDate","04022018")
$CollectionNode = $xmlDoc.CreateElement("ImageViewAnalysis")
$xmlDoc.SelectSingleNode("//FileHeader//Item//ImageViewDetail").AppendChild($CollectionNode)
$CollectionNode.SetAttribute("Status","Pending")
$xmlDoc.Save($XML_Path)
所以我将以下代码段添加到我的代码中
$CollectionNode = $xmlDoc.CreateElement("ImageViewDetail")
$xmlDoc.SelectSingleNode("//FileHeader//Item").AppendChild($CollectionNode)
$CollectionNode = $xmlDoc.CreateElement("ImageViewData")
$xmlDoc.SelectSingleNode("//FileHeader//Item//ImageViewDetail").AppendChild($CollectionNode)
$CollectionNode.SetAttribute("Selected","Yes")
但是它没有在第二个标签中添加,而是在第一个<ImageViewDetail>
标签中添加了它。
还有一个问题:我想在标题为standalone="no"
的标题部分添加另一个属性,
<?xml version="1.0" encoding="utf-8" standalone="no"?>"
如何做到?
答案 0 :(得分:0)
可以像这样选择最后一个<ImageViewDetail>
节点:
$xmlDoc.SelectSingleNode('//ImageViewDetail[last()]')
要选择其他节点,您必须设计一个XPath表达式来匹配要选择的特定节点。
可以这样更改XML声明:
$xmlDoc.ChildNodes[0].Value = 'version="1.0" encoding="utf-8" standalone="no"'