Loading XML into Dataset (.NET)

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Reaction score
13
I have an XML document that looks like:

Code:
<Record>
<RedordID>1</RecordID>
<RecordName>test</RecordName>
<Descriptions>
<Description><Name>This is a description1</Name><Value>This is its value</Value></Description>
<Description><Name>This is a description2</Name><Value>This is its value3</Value></Description>
<Description><Name>This is a description3</Name><Value>This is its value2</Value></Description>
</Descriptions>
</Record>

When loading it into the dataset it seems to throw away the <Description> part. Any ideas why? I need to display/save this and I can't figure out why I can't load them in the dataset
 
Last edited:
I don't think you can load a complex data structure like that into a Dataset. It pretty much has to be rows of data with unique column names. What you have is not going to fit into dataset.
 
Sorry forgot to add:

<Descriptions>
</Descriptions>

encapsulating the XML
 
I don't think you can load a complex data structure like that into a Dataset. It pretty much has to be rows of data with unique column names. What you have is not going to fit into dataset.

Should I use XMLReader instead then? ... mmmm... maybe I should have tried that first before posting :eek:
 
Should I use XMLReader instead then? ... mmmm... maybe I should have tried that first before posting :eek:

Yup, would probably work better. To get the data in your original XML you're going to need a Records dataTable and a Descriptions datatable and a table join between the two.
 
Quickly hacked this together, I hope this helps! :)

I changed your XML a little too, I hope you don't mind

Code:
<Record>
	<RecordID>1</RecordID>
	<RecordName>test</RecordName>
	<Description_Collection>
		<Description>
			<Name>This is a description1</Name>
			<Value>This is its value</Value>
		</Description>
		<Description>
			<Name>This is a description2</Name>
			<Value>This is its value3</Value>
		</Description>
	</Description_Collection>
</Record>

Code:
static void Main(string[] args)
        {
            //Create the DataSet object and load the XML into it, you don't need an XSD to do this.
            DataSet ds = new DataSet();
            ds.ReadXml(@"c:\ds.xml");

            //Basic example of how to access the data using DataRelasionships
            DataRow[] rows = ds.Tables["Record"].Select();
            foreach (DataRow row in rows)
            {
                Console.WriteLine(row["RecordID"]);
                Console.WriteLine(row["RecordName"]);

                DataRelation DescRelationship = ds.Relations["Record_Description_Collection"];
                DataRow[] DescCol = row.GetChildRows(DescRelationship);

                DataRelation DepRelationship = ds.Relations["Description_Collection_Description"];
                DataRow[] DescRows = DescCol[0].GetChildRows(DepRelationship);

                Console.WriteLine(string.Format("{0}{1}", "Desc Lines: ", DescRows.Length));
                Console.WriteLine();
                foreach (DataRow DescRow in DescRows)
                {
                    Console.WriteLine(DescRow["Name"]);
                    Console.WriteLine(DescRow["Value"]);
                    Console.WriteLine();
                }
            }

            Console.ReadLine();
        }
 
Last edited:
why don't you use the xsd tool to create a class from xml schema and then serialize and deserialize the xml data? Or you can use the xsd tool to create the dataset for you and then just load the xml data into the dataset?

I use option 1. It fast and easy and I don't have datasets lying around in my projects, just classes.

Do the following:
Create the xsd shema file:
xsd.exe record.xml

Generate the c#(vb.net) code file:
xsd.exe record.xsd /c /l:cs

Use the Visual Studion command prompt to run the xsd command. I have called the xml data file you specified earlier record.xml, but you can call it abviously whatever you want.
 
Last edited:
why don't you use the xsd tool to create a class from xml schema and then serialize and deserialize the xml data? Or you can use the xsd tool to create the dataset for you and then just load the xml data into the dataset?

I use option 1. It fast and easy and I don't have datasets lying around in my projects, just classes.

Do the following:
Create the xsd shema file:
xsd.exe record.xml

Generate the c#(vb.net) code file:
xsd.exe record.xsd /c /l:cs

Use the Visual Studion command prompt to run the xsd command. I have called the xml data file you specified earlier record.xml, but you can call it abviously whatever you want.

I don't have control over these little suckers

I'm writing a dynamic import tool to handle data feeds. And because the supplier can't be trusted with the data (UTF-8 is a foreign language apparently including some other things... you'd think with all that money they'd hire a decent bunch of programmers?) I have to compensate wherever possible.

Thanks for the advice though
 
Top
Sign up to the MyBroadband newsletter
X