473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parse Nested Elements to Single DataGrid

I have been losing my mind trying to parse an XML document (with nested child
elements, not all of which appear in each parent node) into a DataGrid
object. What I want to do is "flatten" the XML document into a text
document with a single row for each parent node (that has all of the values
from all of the child nodes for that row)

The DataView within VS 2005 IDE displays my 15 or so child tables - and
knows that some parent rows don't have child rows in every relation - but
trying to get all the child tables to go with the correct parent row
programmaticall y is killing me.

Getting the parent node to bind to the DataGrid is simple

dsSource.ReadXm l(CStr(Session( "FilePath") ))

'Bind the XML to a DataGrid
dgMaster.DataSo urce = dsSource
dgMaster.DataBi nd()
dgMaster.Visibl e = True

Of course, we all know that this will only show me dsSource.Tables (0), not
ALL of them

Isn't there an easy way to get the values from all the child nodes to bind
and display on the same DataGrid rather than one separate DataGrid for each
child relation?

Obviously, the DataView within the VS 2005 IDE understands how to infer the
relationships between parent and child nodes (I guess with ParentRelations
and ChildRelations objects - and associated ID's)

I know the DOM is supposed to be able to parse through child nodes, but how
do I do that and get them all linked up to the correct parent row? Right
now the code I have uses all kinds of row counters and all kinds of loops
similar to

For i = 0 To dsSource.Tables .Count - 1

Next

My row counters (and associated arrays to track CurrentRow, LastRow, etc)
are constantly off by one or two rows.

Help!!! I know I missing something, but I'm so far down the rat hole I
can't find my way back out.
Jun 16 '06 #1
5 2959
Hi,

Is the Xml document generated by DataSet.WriteXm l? If so, have you tried to
create a schema, which contains DataRelation information and can create the
map automatically when reading from Xml.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jun 19 '06 #2
I doubt the document is generated by that method, since the website that
produces the XML file is a jsp based app. However, it sounds like your
suggestion might still be an option. (?) Is there any example out there on
MSDN that shows programmaticall y how to create the schema (and map the
information), since I need to automate the process?

Thanks.

"Kevin Yu [MSFT]" wrote:
Hi,

Is the Xml document generated by DataSet.WriteXm l? If so, have you tried to
create a schema, which contains DataRelation information and can create the
map automatically when reading from Xml.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jun 19 '06 #3
Hi,

There are several ways to achieve this.

If the schema is included in the Xml document, you can use
XmlReadMode.Rea dSchema directly to get the schema info.

If the Xml document doesn't include schema, you can use
XmlReadMode.Inf erSchema to let it generate schema for you. However, this is
not 100% reliable for the nested relationship in the table.

The other way is to design a typed DataSet, which contains DataRelation
info.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jun 20 '06 #4
There is no schema included, so it looks like the first step then would be to
use XmlReadMode.Inf erSchema.

Absent that, I've been doing this via an untyped dataset and parse each
child table. I'm going to post the code here, but it sounds like if I could
just find some way to create a typed dataset, I wouldn't need all this
looping and could instead somehow write all the data to a single DataSet that
would display "flat" in a single spreadsheet? Problem is, I cannot figure
out how to create a dataset that does NOT have the nested child relations as
different datasets.

Public Sub PopulateTable_d emo()

'Copied from PopulateTable
Dim i, j, l, x, xTableNum As Integer

'Dim MasterTable As New DataTable
Dim MyCol As DataColumn
Dim MyRow As DataRow
Dim TableRowNum(,) As String
Dim ColumnName As String
dsSource.ReadXm l(CStr(Session( "FilePath") ))

'Before we do ANYTHING, let's delete the PERSON table and all
its child tables from the dataset so we don't store personal info
ReDim TableRowNum(dsS ource.Tables.Co unt - 1, 2)
'Initialize all the values to zero
For i = 0 To dsSource.Tables .Count - 1

TableRowNum(i, 0) = 0
TableRowNum(i, 1) = dsSource.Tables (i).TableName
TableRowNum(i, 2) = 0

Next
'Create mastertable

For i = 0 To dsSource.Tables .Count - 1
For j = 0 To dsSource.Tables (i).Columns.Cou nt - 1

'Need to add two extra columns - 1 for lat, 1 for long
ColumnName = dsSource.Tables (i).TableName &
dsSource.Tables (i).Columns(j). ColumnName

If MasterTable.Col umns.IndexOf(Co lumnName) = -1 Then
'If
MasterTable.Col umns.IndexOf(ds Source.Tables(i ).Columns(j).Co lumnName) = -1
Then
'MyCol = New
DataColumn(dsSo urce.Tables(i). Columns(j).Colu mnName)
MyCol = New DataColumn(Colu mnName)
MyCol.DataType = System.Type.Get Type("System.St ring")
MasterTable.Col umns.Add(MyCol)

End If

Next
Next

'Now populate each row
Dim LoopRowNum
Dim PrimaryKeyName As String
Dim PrimaryColumnVa lue As Integer

Dim CellValue() As Object
Dim k, m, t As Integer
Dim TableName, TableNameInner As String
Dim TableEntry(,) As String
Dim ProcessTable As Boolean
Dim ParentTableName As String
Dim ParentKeyName As String
'This holds values for whether child table was checked already
for current row j
ReDim TableEntry(dsSo urce.Tables.Cou nt - 1, 1)
For j = 0 To dsSource.Tables (0).Rows.Count - 1

'Reinitialize TableEntry array - haven't hit any child
tables yet for this row
For t = 0 To dsSource.Tables .Count - 1
TableEntry(t, 0) = False
TableEntry(t, 1) = ""
Next

MyRow = MasterTable.New Row()

'Dont think I need this?
'Get value of parent id
'Counter of PrimaryKeyRow equals row of master table, which
is simply "j"

For l = 0 To dsSource.Tables (0).Columns.Cou nt - 1
ColumnName = dsSource.Tables (0).TableName &
dsSource.Tables (0).Rows(j).Tab le.Columns(l).C olumnName

'btm16jun-MyRow.Item(dsSo urce.Tables(0). Rows(j).Table.C olumns(l).Colum nName)
= dsSource.Tables (0).Rows(j).Ite m(l)
MyRow.Item(Colu mnName) =
dsSource.Tables (0).Rows(j).Ite m(l)

Next
'Save the next and last row values for this parent
TableRowNum(0, 0) = j + 1
TableRowNum(0, 2) = j

For i = 1 To dsSource.Tables .Count - 1
Dim ChildCount As Integer

CellValue = Nothing

TableName = dsSource.Tables (i).TableName

If
CStr(dsSource.T ables("Event"). Rows(j).Item("I nternalID")) = "9068" Then
If TableName = "VehiclesInvolv ed" Then
Dim test1 As String = "test"
End If
End If
'Always hit table 1, so skip this checking the
TableEntry Array

'Only hit this table if it hasn't been hit already for
this row
ProcessTable = True
For x = 1 To dsSource.Tables .Count - 1
'For m = 0 To dsSource.Tables .Count - 2
If TableEntry(x, 1) = TableName And
CBool(TableEntr y(x, 0)) = True Then
ProcessTable = False
Exit For
End If

Next
'end skip TableEntry Array

If ProcessTable = True Then
'Get LoopRowNum for table where TableName is the key
LoopRowNum = CInt(TableRowNu m(i, 0))

'Save off the current row value into i,2
TableRowNum(i, 2) = LoopRowNum

''Don't need this - we have the parent primary key...

'Gotta check to see who the parent table is...
ParentTableName =
dsSource.Tables (TableName).Par entRelations.It em(0).ParentTab le.TableName

ParentKeyName =
dsSource.Tables (TableName).Par entRelations.It em(0).ParentCol umns(0).ColumnN ame

'Get parent column value of parent table - have to
choose the right parent row num (TableRowNum(0, i))
For x = 0 To dsSource.Tables .Count - 1
If TableRowNum(x, 1) = ParentTableName Then
'PrimaryColumnV alue =
dsSource.Tables (ParentTableNam e).Rows(TableRo wNum(0, x)).Item(Parent KeyName)
'Maybe the 2,x goes here?
PrimaryColumnVa lue =
dsSource.Tables (ParentTableNam e).Rows(TableRo wNum(x, 2)).Item(Parent KeyName)
Exit For
End If
Next

'Now that we know name of primary key column, keep
checking it until row number changes
'Create Array to hold Nolumn values - appending
multiple row values - in each cell
If LoopRowNum <
dsSource.Tables (TableName).Row s.Count Then
ReDim
CellValue(dsSou rce.Tables(Tabl eName).Columns. Count - 1)

'btm16jun-If
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(ParentKeyNa me).GetType.Nam e
<> "DBNull" Then
Dim LoopVal As Integer
If
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(ParentKeyNa me).GetType.Nam e
= "DBNull" Then
LoopVal = 0
Else
LoopVal =
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(ParentKeyNa me)
End If
Do Until LoopVal <> PrimaryColumnVa lue
'Do Until
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(ParentKeyNa me) <>
PrimaryColumnVa lue

For l = 0 To
dsSource.Tables (TableName).Col umns.Count - 1
If
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(l).GetType. Name() = "DBNull"
Then
CellValue(l) = CellValue(l) & ""
Else
CellValue(l) = CellValue(l) &
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(l) & vbCrLf
End If

Next

'Save off tableRowNum in case we just hit
the last row...

'now increment it
LoopRowNum = LoopRowNum + 1

'Save off tableRowNum in case we just hit
the last row...
TableRowNum(i, 0) = LoopRowNum

'Only keep looping if we're not at max row
count
If LoopRowNum =
dsSource.Tables (TableName).Row s.Count Then
Exit Do
End If

'2.0-BTM - might also be a null now that
were looking at next row, if so, exit?
If
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(ParentKeyNa me).GetType.Nam e
= "DBNull" Then
Exit Do
End If
LoopVal =
dsSource.Tables (TableName).Row s(LoopRowNum).I tem(ParentKeyNa me)

Loop

'Flag TableEntry (for TableName) - as being
complete for this row - don't come back to this table again until next master
row (j)
TableEntry(i, 0) = True
TableEntry(i, 1) = TableName

'Add the items to the row - if we had any rows
to add
If LoopRowNum > CInt(TableRowNu m(i, 2)) Then

For l = 0 To
dsSource.Tables (TableName).Col umns.Count - 1

'Again, row(0) for getting the column
name is fine
ColumnName = TableName &
dsSource.Tables (TableName).Row s(0).Table.Colu mns(l).ColumnNa me

'btm16Jun-MyRow.Item(dsSo urce.Tables(Tab leName).Rows(0) .Table.Columns( l).ColumnName) = CellValue(l)
MyRow.Item(Colu mnName) = CellValue(l)

Next
End If
'btm-16Jun-End If
End If

Else
'we have nothing to add to this table since were
past the last row, just flag as complete so we don't hit it again
TableEntry(i, 0) = True
TableEntry(i, 1) = TableName

End If

''put new end if here

'Now see if there are child tables to Table(1)

'If count is zero, then parent is Table(0), process row
now
If dsSource.Tables (TableName).Chi ldRelations.Cou nt > 0
Then

ChildCount =
dsSource.Tables (TableName).Chi ldRelations.Cou nt - 1
'First get value of ParentColumn ID

PrimaryKeyName =
dsSource.Tables (TableName).Chi ldRelations.Ite m(0).ParentColu mns(0).ColumnNa me

''''Get value that is in the parent column
''''Get PrimaryKeyValue - have to choose the current
row num (current value in TableRowNum(0,x ))
For x = 0 To dsSource.Tables .Count - 1
If TableRowNum(x, 1) = TableName Then
'Again, we may already have processed last
row in this table, if so, don't go get primary column - as we shouldn't be
processing any more child rows either
If TableRowNum(x, 2) <
dsSource.Tables (TableName).Row s.Count Then
PrimaryColumnVa lue =
dsSource.Tables (TableName).Row s(TableRowNum(x , 2)).Item(Primar yKeyName)
End If

Exit For
End If
Next

For k = 0 To ChildCount

TableNameInner =
dsSource.Tables (TableName).Chi ldRelations.Ite m(k).ChildTable .TableName

'Only hit this table if it hasn't been hit
already for this row
ProcessTable = True
For x = 1 To dsSource.Tables .Count - 1
'For m = 0 To dsSource.Tables .Count - 2
If TableEntry(x, 1) = TableNameInner And
CBool(TableEntr y(x, 0)) = True Then
ProcessTable = False
Exit For
End If

Next

If ProcessTable = True Then

CellValue = Nothing
'Here, we're not necessarily on Table(i), so
ref by tablename instead
For x = 1 To dsSource.Tables .Count - 1
If TableRowNum(x, 1) = TableNameInner Then
xTableNum = x
Exit For
End If
Next

LoopRowNum = CInt(TableRowNu m(xTableNum, 0))

'Again, save current row in x,2
TableRowNum(xTa bleNum, 2) = LoopRowNum

'Get primary key column name for reference
'Now that we know name of primary key
column, keep checking it until row number changes
'Create Array to hold Nolumn values -
appending multiple row values - in each cell

ParentKeyName =
dsSource.Tables (TableNameInner ).ParentRelatio ns.Item(0).Pare ntColumns(0).Co lumnName

If LoopRowNum <
dsSource.Tables (TableNameInner ).Rows.Count Then
ReDim
CellValue(dsSou rce.Tables(Tabl eNameInner).Col umns.Count - 1)
'btm16jun-If
dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(Parent KeyName).GetTyp e.Name <> "DBNull" Then
Dim LoopVal As Integer
If
dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(Parent KeyName).GetTyp e.Name = "DBNull" Then
LoopVal = 0
Else
LoopVal =
dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(Parent KeyName)
End If
Do Until LoopVal <> PrimaryColumnVa lue

If LoopRowNum =
dsSource.Tables (TableNameInner ).Rows.Count Then
Exit Do
End If

For l = 0 To
dsSource.Tables (TableNameInner ).Columns.Count - 1
If
dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(l).Get Type.Name() =
"DBNull" Then
CellValue(l) = CellValue(l)
& ""
Else
CellValue(l) = CellValue(l)
& dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(l) & vbCrLf
End If
Next

LoopRowNum = LoopRowNum + 1

TableRowNum(xTa bleNum, 0) = LoopRowNum
'Only keep looping if we're not at
max row count
If LoopRowNum =
dsSource.Tables (TableNameInner ).Rows.Count Then
Exit Do

End If

'2.0-BTM - might also be a null now
that were looking at next row, if so, exit?
If
dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(Parent KeyName).GetTyp e.Name = "DBNull" Then
Exit Do
End If
LoopVal =
dsSource.Tables (TableNameInner ).Rows(LoopRowN um).Item(Parent KeyName)
Loop
'Save off row counter of that table to
array again - start there next time for master row (j)

'Flag TableEntry (for TableName) - as
being complete for this row - don't come back to this table again until next
master row (j)
TableEntry(xTab leNum, 0) = True
TableEntry(xTab leNum, 1) = TableNameInner

'Add the items to the row - if we had
any rows to add
If LoopRowNum >
CInt(TableRowNu m(xTableNum, 2)) Then
'Add the items to the row
For l = 0 To
dsSource.Tables (TableNameInner ).Columns.Count - 1

'Again, row(0) for getting the
column name is fine
ColumnName = TableNameInner &
dsSource.Tables (TableNameInner ).Rows(0).Table .Columns(l).Col umnName

'btm16Jun-MyRow.Item(dsSo urce.Tables(Tab leNameInner).Ro ws(0).Table.Col umns(l).ColumnN ame) = CellValue(l)
MyRow.Item(Colu mnName) =
CellValue(l)

Next
End If
'End If
'btm16Jun-end
Else
'were already past last row of this
table, be sure to flag as complete so we don't hit it again
TableEntry(xTab leNum, 0) = True
TableEntry(xTab leNum, 1) = TableNameInner
End If

'End If
'btm16Jun end

End If

Next

End If
''end fix attempt

Next
MasterTable.Row s.Add(MyRow)
Next

BindDataGrid()
End Sub

"Kevin Yu [MSFT]" wrote:
Hi,

There are several ways to achieve this.

If the schema is included in the Xml document, you can use
XmlReadMode.Rea dSchema directly to get the schema info.

If the Xml document doesn't include schema, you can use
XmlReadMode.Inf erSchema to let it generate schema for you. However, this is
not 100% reliable for the nested relationship in the table.

The other way is to design a typed DataSet, which contains DataRelation
info.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jun 20 '06 #5
Hi,

I'm not quite sure what you mean by createing a dataset that "does NOT have
the nested child relations as different datasets". I think creating a
DataSet with nested relation is just fine and you can read xml data into
that dataset. Then the whole structure will be created.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jun 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1779
by: Ike | last post by:
I am hoping someone can help me with the proper syntax for this. I have an attribute, called, say "name," such that: <set name="something">thename</set> However, the value for name, is something that is unknown, something within tags itself. So, for example, the tag <star index="1"/> might be "something." How then can I express, or...
2
945
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child datagrid. HTML Datagrid1 TemplateColumn Table Header information Detail Information
2
1691
by: nulldevice | last post by:
I've got a datagrid with some nested controls, created at design-time. The controls themselves have no databound values. After a few other operations on the page (selecting a few parameters, etc), a button is clicked and the databind occurs. This part works fine. It retreives the 4-10 rows it needs and using the itemdatabound formats...
0
1689
by: Chris | last post by:
I've been searching all over and think I am close, but keep getting the error "Index out of range" when trying to reference a nested datagrid when an OnEditCommand event is raised. When the OnEditCommand event is raised, I try to do the following: 1. Find the selected datalist item in which the nested datagrid raised the event. (by...
6
1735
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost datagrid's OnItemDataBound() method we check for the row in which it's appropriate to add the secondary datagrid. Exactly one row in the topmost grid will...
16
3783
by: danep | last post by:
Hi all, On my site I have a section of code that resembles the following: <p id="gear" style="display: none;"> <p>test</p> </p> This renders fine in Firefox (that is, nothing is displayed). However, in Internet Explorer, "test" is still printed. It appears
2
5550
by: Joseph Geretz | last post by:
When I create a Form, the VB IDE creates the following files in the following hierarchy: Form1.cs Form1.Designer.cs Form1.resx Both Form1.cs and Form1.Designer.cs are partial implementations of a single physical class definition. The VB IDE properly recognizes Form1.cs as a Form (i.e. double-clicking opens the Form designer). It also...
1
64071
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
1
2421
by: Henrik Bechmann | last post by:
All, I'm trying to spoof Google's vertical tabs in a vertical menu structured with nested UL/LI elements. To do this, I need to find out where the anchor in the LI is, and then create an absolute positioned div to bridge the space between the menu and the content page. This works with one level of LI's. However, with more than one, the
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.