Specific elements from XML string to a datagridview or datatable (C#)

nifetaiva

New Member
I've only just recently begun working with XML strings. What I'm doing now is using a webservice to pull records from a database. It returns the following output:\[code\]<?xml version="1.0" encoding="utf-16"?><Records count="2"> <Metadata> <FieldDefinitions> <FieldDefinition id="13597" name="Statement" alias="Statement" /> <FieldDefinition id="13598" name="Response" alias="Response" /> </FieldDefinitions> </Metadata> <Record contentId="154321" moduleId="409"> <Field id="13597" type="1">This is a database record</Field> <Field id="13598" type="1">This is a corresponding record</Field> </Record> <Record contentId="154755" moduleId="409"> <Field id="13597" type="1">This is another database record</Field> <Field id="13598" type="1">And another corresponding record</Field> </Record></Records>\[/code\]What I'm trying to achieve is a datatable/datagridview that will be displayed to the user. Which would be something like this:\[code\]| Statement | Response ||This is a datab..| This is a corr.||This is another..| And another....|\[/code\]I've tried the following code:\[code\] private void WriteDataGrid(string xml) { DataSet ds = new DataSet(); XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Document, null); ds.ReadXml(reader); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Field"; }\[/code\]However it only managed to obtain this:\[code\]| id | type | Field_Text ||13597 | 1 | This is a... ||13598 | 1 | This is a corr...||13597 | 1 | This is anoth... ||13598 | 1 | And anothe... |\[/code\]I plan to use the field id's from the xml to create unique columns, then set the values of the rows based on the enclosed values. But I'm unsure of how I should go about doing this. Could anyone help? Or is there a better way for me to achieve what I'm looking to do?I'm aware it'd be much easier if the Elements were unique, but I'm unable to alter the webservices' output in any way. Any help would be much appreciated.
 
Back
Top