XML Reader

35 views
Skip to first unread message

Colin B

unread,
Jan 3, 2012, 1:47:44 PM1/3/12
to CSVChat
Hi Bruce,

I have an XML file that stores table schemas in this basic format:-

<?xml version="1.0" encoding="UTF-8"?>
<Schema>
<Table>
<TableName>account</TableName>
<Column>
<ColumnName>account_id</ColumnName>
<Type>numeric</Type>
<Length>9</Length>
</Column>
<Column>
<ColumnName>account_bal</ColumnName>
<Type>money</Type>
<Length>8</Length>
</Column>
</Table>
<Table>
<TableName>accounts</TableName>
<Column>
<ColumnName>patient_id</ColumnName>
<Type>numeric</Type>
<Length>9</Length>
</Column>
</Table>
</Schema>

Can you tell me the best way to read this please as I want to create
some sort of datatable with:-

TableName, Column Name, Column Type, Column Size

I've tried various methods, but the only way I could get it to work is
to add the TableName to part of the <Column> record and removing the
<Table>, then opening up the XMLREcordReader with "Schema/Column" not
sure if this is very efficient. I did create the XML, so if you feel I
should be using a different structure, I can always change it.

Thanks in advance
Col




shriop

unread,
Jan 3, 2012, 11:46:56 PM1/3/12
to CSVChat
The way you structured your xml makes sense from an xml perspective,
but it requires parent references based on the way you have to set up
the loop xpath. Parent references aren't allowed when the load method
is set to streaming, which is the default, which is what allows any
size xml file to be parsed normally. I'm assuming loading your entire
file into memory won't be an issue based on the content.

using (XmlRecordReader reader = new XmlRecordReader(@"C:\Temp
\tables.xml", "Schema/Table/Column", LoadMethod.InMemory)){
reader.Columns.Add("../TableName", "TableName");
reader.Columns.Add("ColumnName", "Column Name");
reader.Columns.Add("Type", "Column Type");
reader.Columns.Add("Length", "Column Size");
dataGrid1.DataSource = reader.ReadToEnd();}

Bruce Dunwiddie
Reply all
Reply to author
Forward
0 new messages