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>
</catalog>

In the following xml i want to add a node before the node genre with value

<bookSold usa='50' Europe='50'> 1000 </booksold>".

1 Answer 1

1

Here is an example

$x = [xml]@'
<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>
</catalog>
'@

$newNode = $x.CreateElement('bookSold')
$newNode.InnerText = 1000
$newNode.SetAttribute('usa', 50)
$newNode.SetAttribute('Europe', 50)
[void]$x.DocumentElement.SelectSingleNode('/catalog/book').InsertBefore(
    $newNode,
    $x.DocumentElement.SelectSingleNode('/catalog/book/genre'))

$document = [System.Xml.XmlDocument]::new()
#PS4 and lower: $document = New-Object -TypeName 'System.Xml.XmlDocument' -ArgumentList @()
$document.Load('S:\SCRIPTS\XMLTest\xmlTest.xml')

$newNode = $document.CreateElement('bookSold')
$newNode.InnerText = 1000
$newNode.SetAttribute('usa', 50)
$newNode.SetAttribute('Europe', 50)

[void]$document.DocumentElement.SelectSingleNode('/catalog/book').InsertBefore(
    $newNode,
    $document.DocumentElement.SelectSingleNode('/catalog/book/genre'))

$document.Save('S:\SCRIPTS\XMLTest\xmlTest.xml')
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. this works. can you please explain [void] and DocumenElement is necessary?
[void] is same as ... | Out-Null - it supresses return value. InsertBefore returns a reference to XmlNode being inserted, and we do not need it. All output that was not catched by powershell, will be printed to powershell console. [void] catches it and redirects to nowhere. DocumentElement is not necessary, but preferred - it can avoid you working with bad-formatted xml documents. It is a "root node" of your XML document, while $x is your document itself. $x contains methods for document like Load, Save etc, and DocumentElement is just an XmlNode.
Thanks for all the help. I may be asking too much but could you recommend any online course that would help me build my power shell skills because currently i'm a beginner and i need to clear basics
Almost everything you can do with c#, you can do with powershell. Powershell is just a wrapper around .net (c#). So on any problem, you can google "How to ... in c#", and translate it to powershell. You even can use third-party .net libraries. See, I updated answer. I've created document using pure .net way [System.Xml.XmlDocument]::new() (for powershell 4 and lower there is alternative syntax, that is uglier, but more universal)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.