Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / February 2008

Tip: Looking for answers? Try searching our database.

Flatten XML file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ruaidhri - 13 Feb 2008 22:07 GMT
Hi,

I have a large xml file that contains many nested nodes that I want to
convert into a flat structure so I can read it into a single database
table.

I am using VB.Net / SQL Server 2000.

A simple example looks something like this (in my xml file there is
about 15 levels of nesting):

ParentVal1
  ChildVal1
  ChildVal2
       GrandChildVal1
       GrandChildVal2
ParentVal2
  ChildVal1
       GrandChildVal1
       GrandChildVal2

I need to convert this to:

ParentVal1 ChildVal1 blank
ParentVal1 ChildVal2 GrandChildVal1
ParentVal1 ChildVal2 GrandChildVal2
ParentVal2 ChildVal1 GrandChildVal1
ParentVal2 ChildVal1 GrandChildVal2

What is the best way to achieve this?

I can read the xml file into a dataset.  But then the dataset will
contain 15 tables.  Do I need to loop through the tables and write the
results to another dataset perhaps!

Alternatively I can read the file using the XmlTextReader class.  Then
I have to loop through the data evaluating node types etc.  I still
have the problem of trying to create a single record from the nested
structure.

Finally, I have a xsd schema file. Do I gain anything by using the
schema file apart from just validating that the xml file fits its
structure?

Thanks in advance.
Anthony Jones - 13 Feb 2008 23:18 GMT
> Hi,
>
[quoted text clipped - 39 lines]
> schema file apart from just validating that the xml file fits its
> structure?

Define large?

Perhaps SQL Server's own OPENXML can handle this for you.

Could you give use a more realistic sample of XML?

Signature

Anthony Jones - MVP ASP/ASP.NET

ruaidhri - 14 Feb 2008 08:10 GMT
The files are over 150MBs.

I am trying to build something that will be generic, something that I
can use for any xml file.  I want to write the data as rows to a
database table with columns; col1, col2, col3 etc.  I do not think
that OPENXML will work for me.

 <Nam>ABC</Nam>
 <envText>P</envText>
 <rptC>XY123</rptCd>
 <rptNam>Detail</rptNam>
 <membNam>MY COMPANY</membNam>
 <rptPrntEDat>2008-01-31</rptPrntEDat>
 <rptPrntRDat>2008-01-31</rptPrntRDat>
 </rptHdr>
- <cb999Grp>
- <cb999KeyGrp>
 <membClgIdCod>MISC</membClgIdCod>
 </cb999KeyGrp>
- <cb999Grp1>
- <cb999KeyGrp1>
 <membxIdCod>PPLO</membxIdCod>
 </cb999KeyGrp1>
- <cb999Grp2>
- <cb999KeyGrp2>
 <currTypCod>EUR</currTypCod>
 </cb999KeyGrp2>
- <cb999Grp3>
- <cb999KeyGrp3>
 <acctTypGrp>A1</acctTypGrp>
 </cb999KeyGrp3>
- <cb999Grp4>
- <cb999KeyGrp4>
 <prodTypId>FDOO</prodTypId>
 <prodId>GBHH</prodId>
 </cb999KeyGrp4>
 <fSiz>0.01</fSiz>
 <fVal>10.0000</fVal>
- <cb999Grp5>
- <cb999KeyGrp5>
- <cntrDtlClassGrp>
 <cntrClasCod>C</cntrClasCod>
- <cntrDtlGrp>
 <cntrpoMthDat>3</cntrpoMthDat>
 <cntrpoYrDat>2008</cntrpoYrDat>

....and it keeps going. For this file, the depth is 15 levels.

Any help would be appreciated.
Anthony Jones - 14 Feb 2008 09:26 GMT
> The files are over 150MBs.
>
[quoted text clipped - 43 lines]
>
> ....and it keeps going. For this file, the depth is 15 levels.

Use a SqlCommand that uses OPENXML to parse and insert the data for a single
record.

Use System.Xml.XmlTextReader will allow you parse throught this Xml file as
stream. move passed the root node(s) until you are at the first node that
represents a record.  Use ReadOuterXml to retrieve an XML string for just
one record and execute your command.

In this approach you never need to load the whole thing in memory.

Signature

Anthony Jones - MVP ASP/ASP.NET


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.