0

I have an xml file as given below

<Books>
  <Book rev="19ver" nver="1.0.0.0" >
    <Book rev="19Sub4" nver="1.4.0.250">
      <Book rev="19Sub5" nver="1.5.0.250" >
    <Book rev="19Sub5Fix1" nver="1.5.1.250" >
    </Book>
  </Book>
</Book>
<Book rev="20ver" nver="2.0.0.0" >
  <Book rev="20Sub1" nver="2.1.0.769" >
    <Book rev="20Sub1Fix7" nver="2.1.7.769"  >
    </Book>
    <Book rev="20Sub1Fix6" nver="2.1.6.769"  >
    </Book>
    <Book rev="20Sub1Fix4" nver="2.1.4.769"  >
    </Book>
    <Book rev="20Sub1Fix3" nver="2.1.3.769"  >
    </Book>
    <Book rev="20Sub1Fix2" nver="2.1.2.769"  >
    </Book>
    <Book rev="20Sub1Fix1" nver="2.1.1.769"  >
    </Book>
  </Book>
  <Book rev="20Sub3" nver="2.3.0.1111">
    <Book rev="20Sub3Fix5" nver="2.3.5.1111"  >
    </Book>
    <Book rev="20Sub3Fix4" nver="2.3.4.1111"  >
    </Book>
     <Book rev="20Sub3Fix3" nver="2.3.3.1111"  >
    </Book>
    <Book rev="20Sub3Fix2" nver="2.3.2.1111"  >
    </Book>
    <Book rev="20Sub4" nver="2.4.0.1567" >
      <Book rev="20Sub4Fix5" nver="2.4.5.1567"  >
      </Book>
      <Book rev="20Sub4Fix4" nver="2.4.4.1567"  >
      </Book>
      <Book rev="20Sub4Fix3" nver="2.4.3.1567"  >
      </Book>
      <Book rev="20Sub4Fix2" nver="2.4.2.1567"  >
      </Book>
      <Book rev="20Sub4Fix1" nver="2.4.1.1567"  >
      </Book>
    </Book>
</Book>
 </Book>
</Book>
 </Books>

I have a powershell script to add a new element inside the specified node. For example if i want to enter the element with rev "20Sub3Fix6" and nver "2.3.6.1111" inside "20Sub3" i can use the following code.

$parentXML = Select-Xml -Xml $bookInfo -XPath "//*[@rev='20Sub3']"
$bookInfo = Get-Content -Raw -Path $XMLPATH
$book = $bookInfo.CreateNode("element","book","")             
$book.SetAttribute("rev","20Sub3Fix6")
$book.SetAttribute("nver","2.3.6.1111")
$rslt = $parentXML.Node.AppendChild($book)
$bookInfo.Save($XMLPATH)

What I'm looking is that when I add a new element it should come in given order. That means "20Sub3Fix6" element should come on top of ""20Sub3Fix5" element. But now it is coming after "20Sub3Fix2".

The expcted XML format is given below

<Book rev="20Sub3" nver="2.3.0.1111">
    <Book rev="20Sub3Fix6" nver="2.3.6.1111"  >
    </Book>
    <Book rev="20Sub3Fix5" nver="2.3.5.1111"  >
    </Book>
    <Book rev="20Sub3Fix4" nver="2.3.4.1111"  >
    </Book>
     <Book rev="20Sub3Fix3" nver="2.3.3.1111"  >
    </Book>
    <Book rev="20Sub3Fix2" nver="2.3.2.1111"  >
    </Book>
    </Book>

What changes I should made in my code so that the i will get the expected XML format?

3
  • I don't see anything C# / .net related. Why tag it? Commented Jun 15, 2021 at 7:56
  • 1
    Powershell support .net objects. If you know the answer in C# then post that i can convert it to powershell Commented Jun 15, 2021 at 8:44
  • Ah, I see. Maybe drop a line about that fact in the question. Commented Jun 15, 2021 at 8:51

1 Answer 1

3

Use PrependChild instead of AppendChild.

Sign up to request clarification or add additional context in comments.

Comments

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.