Help with changing order of data element using VB.NET

Hi,

How do you loop through an xml file and change the order of a data element? For example if my xml file contains

<Root>
<Table1>
<ID>1</ID>
<Item1><Item1>
<Item3><Item3>
<Item4><Item4>
<Item2><Item2>
</Table1>
</Root>

I would like to move the last data element (Item2) below the Item1 data element.

<Root>
<Table1>
<ID>1</ID>
<Item1><Item1>
<Item2><Item2>
<Item3><Item3>
<Item4><Item4>
</Table1>
</Root>

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Victor;

This should do it.

'' Locations of where to read and save the XML files
Dim inputXML As String = "Path and file name to XML file"
Dim saveXML As String = "Path and file name of where to save XML file"

'' Load XML document
Dim xdoc As XDocument = XDocument.Load(inputXML)

'' Order the ItemX in each Table1 node
Dim results = (From n In xdoc.Descendants("Table1") _
               Let items = n.Elements().OrderBy (Function(x) x.Name.ToString() ) _
               select n, items ).ToList()

'' Update the xdoc 
results.ForEach( Sub(node) node.n.ReplaceAll(node.items))

'' Save to the file system
xdoc.Save(saveXML)

Open in new window

0
vcharlesAuthor Commented:
Hi Fernando,

The names I gave was in example, in my project they are different.

<Root>
 <Table1>
 <ID>1</ID>
 <NSC><NSC>
 <NSN><NSN>
 <FIF><FIF>
 <AGD><AGD>
 </Table1>
 </Root>

Would like to move AGD under NSC.

Thanks,

Victor
0
Fernando SotoRetiredCommented:
Hi Victor;

Working with XML documents or fragments is very dependent on the XML schema/structure and so asking questions really requires the actual names of node and the structure of the document and if multiple nodes are involved.

Is it possible for the XML to have more then one Table1 node with multiple AGD node that have to move within their own parents node beneath NSC node?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

vcharlesAuthor Commented:
Hi Fernando,

Yes, the xml file contains multiple records for <table1>. For example

<Root>
  <Table1>
  <ID>1</ID>
  <NSC><NSC>
  <NSN><NSN>
  <FIF><FIF>
  <AGD><AGD>
  </Table1>
<Table1>
  <ID>2</ID>
  <NSC><NSC>
  <NSN><NSN>
  <FIF><FIF>
  <AGD><AGD>
  </Table1>
<Table1>
  <ID>3</ID>
  <NSC><NSC>
  <NSN><NSN>
  <FIF><FIF>
  <AGD><AGD>
  </Table1>
<Table1>
  <ID>4</ID>
  <NSC><NSC>
  <NSN><NSN>
  <FIF><FIF>
  <AGD><AGD>
  </Table1>
  </Root>

Thanks,

Victor
0
Fernando SotoRetiredCommented:
Hi Victor;

This code snippet should give you what you need.

'' Load the XML document
Dim xdoc = XDocument.Load(".... .xml")

'' Query document for Table1 nodes with both child elements
Dim results = (From n In xdoc.Descendants("Table1") _
               Where n.Element("NSC") IsNot Nothing AndAlso n.Element("AGD") IsNot Nothing _
               Select n ).ToList()

'' Move node AGD after NSC
For Each n As XElement In results
    Dim agd = n.Element("AGD") 
    n.Element("AGD").Remove()
    n.Element("NSC").AddAfterSelf(agd)
Next

xdoc.Save(" ... .xml")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thanks.
0
vcharlesAuthor Commented:
Thank You!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.