Help with creating multiple xml files from one xml files

Hi,

If I have an xml file (Part A) how do I create multiple xml files in Part B using VB.NET? In my actual project the data elements will be in different order, tehrefore I will have to specify which data elements I want to inlcude in each file.


Part A:

FileAll.xml

<Root>
<Table>
<ID>1<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
<NSCA></NSCA>
<NSNA></NSNA>
<NOSA></NOSA>
<AGSA></AGSA>
<NSCB></NSCB>
<NSNB></NSNB>
<NOSB></NOSB>
<AGSB></AGSB>
</Table>
<Table>
<ID>2<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
<NSCA></NSCA>
<NSNA></NSNA>
<NOSA></NOSA>
<AGSA></AGSA>
<NSCB></NSCB>
<NSNB></NSNB>
<NOSB></NOSB>
<AGSB></AGSB>
</Table>
<Table>
<ID>3<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
<NSCA></NSCA>
<NSNA></NSNA>
<NOSA></NOSA>
<AGSA></AGSA>
<NSCB></NSCB>
<NSNB></NSNB>
<NOSB></NOSB>
<AGSB></AGSB>
</Table>
</Root>

Part B:

File1.xml

<Root>
<Table>
<ID>1<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
</Table>
<Table>
<ID>2<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
</Table>
<Table>
<ID>3<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
</Table>
</Root>

File2.xml

<Root>
<Table>
<ID>1<./ID>
<NSCA></NSCA>
<NSNA></NSNA>
<NOSA></NOSA>
<AGSA></AGSA>
</Table>
<Table>
<ID>2<./ID>
<NSCA></NSCA>
<NSNA></NSNA>
<NOSA></NOSA>
<AGSA></AGSA>
</Table>
<Table>
<ID>3<./ID>
<NSCA></NSCA>
<NSNA></NSNA>
<NOSA></NOSA>
<AGSA></AGSA>
</Table>
</Root>

File3.xml

<Root>
<Table>
<ID>1<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
</Table>
<Table>
<ID>2<./ID>
<NSC></NSC>
<NSN></NSN>
<NOS></NOS>
<AGS></AGS>
</Table>
<Table>
<ID>3<./ID>
<NSCB></NSCB>
<NSNB></NSNB>
<NOSB></NOSB>
<AGSB></AGSB>
</Table>
</Root>
vcharlesAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi Victor;

This will do what your last post asked for.

Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\SplitToMultipleFiles.xml")
Dim donotIncludeNodes As New List(Of String) From {"NSC", "NOS", "NSN", "AGS"}

Dim result = (From node In xdoc.Descendants()
              Where donotIncludeNodes.Contains(node.Name.ToString())
              Select node).ToList()

result.ForEach(Sub(node As XElement) node.Remove())

'' At this point xdoc will have all those nodes removed

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi Victor;

Can you explain in words exactly how to create the three new XML files. The example just does not click for me.
0
 
vcharlesAuthor Commented:
Hi Fernando,
I would like to create new xml files using list of data elements (i.e. fielda, fieldb, filedc) I would like to see. For example for file1.xml I only want to create an xml file with the fields shown in file1.xml. Another approach  could be to delete all data elements not included in the  list I want to see and rename the file.
Thanks,
Victor
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Fernando SotoRetiredCommented:
Sorry but I can not come up with an algorithm to do what you want because I really don't understand the logic behind the problem. Hopefully someone else will check in and post a solution.
0
 
vcharlesAuthor Commented:
Hi Fernando,

Is there a way to delete all data elements  in FileAll.xml  included  a string? For example if i have a string s = NSC,NOS,NSN,AGS ? the new xml file created should not include these data elements. This could be another approach to select data elements to create another xml file

Thsnks,

Victof
0
 
vcharlesAuthor Commented:
Thank You.
0
 
vcharlesAuthor Commented:
Thank You.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.