• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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
0
vcharles
Asked:
vcharles
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
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 SotoCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 SotoCommented:
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
 
vcharlesAuthor Commented:
Thanks.
0
 
vcharlesAuthor Commented:
Thank You!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now