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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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;

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
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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

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:
Thank You.
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.