2

次の XML に子ノードを追加しようとしています。できますが、私の問題は最後に追加することです。<catalog>との間の先頭にノードを追加するにはどうすればよいです<book>か?

<?xml version="1.0"?>
 <catalog>
 <book id="bk101">
   <author>Gambardella, Matthew</author>
   <title>XML Developer's Guide</title>
   <genre>Computer</genre>
   <price>44.95</price>
   <publish_date>2000-10-01</publish_date>
   <description>An in-depth look at creating applications 
   with XML.</description>
 </book>
 <book id="bk102">
   <author>Ralls, Kim</author>
   <title>Midnight Rain</title>
   <genre>Fantasy</genre>
   <price>5.95</price>
   <publish_date>2000-12-16</publish_date>
   <description>A former architect battles corporate zombies, 
   an evil sorceress, and her own childhood to become queen 
   of the world.</description>
  </book>
  </catalog>

私のコードは次のとおりです。

 [xml]$a = Get-Content 'C:\Users\me\Documents\Scripts\books.xml'
 $ammend =$a.CreateElement("Quarter")
 $a.DocumentElement.AppendChild($ammend)
 $a.save('C:\Users\me\Documents\Scripts\books.xml')
4

2 に答える 2

2

<catalog>はルート ノードであるため、その前に要素を配置することはできません。ルート ノードが 2 つあると、解析できない無効な XML が発生するためです。

于 2016-08-10T13:25:20.580 に答える