¿Cómo mostrar datos XML en ASP.Net gridview?

Experts ! i am very new to XML. However, a webapp collects data in XML form and i have to display it (and enable edit) in an ASP.Net GridView. i need your help, please.

<?xml version="1.0"?>
<Answers>
  <AnswerSet>
<Answer questionId="MRN">4444</Answer>
<Answer questionId="FName">test</Answer>
<Answer questionId="LName">patient</Answer>
<Answer questionId="AddressPt">blah blah</Answer>
<Answer questionId="Governorate">xxxx</Answer>
<Answer questionId="InitialCSF">Negative</Answer>
<Answer questionId="Diagnosis"></Answer>
<Answer questionId="Description">
</Answer>
  </AnswerSet>
  <AnswerSet>
<Answer questionId="MRN">1</Answer>
<Answer questionId="FName">1</Answer>
<Answer questionId="LName">1</Answer>
<Answer questionId="AddressPt">1</Answer>
<Answer questionId="InitialCSF">Positive</Answer>
<Answer questionId="Diagnosis">dx</Answer>
<Answer questionId="Description">
</Answer>
  </AnswerSet>
 </Answers>

preguntado el 08 de noviembre de 11 a las 19:11

1 Respuestas

Easiest way I can think of to bind the XML to a gridview is to to load the xml to a DataSet and then bind the DataSet to the gridview:

StringReader sr= new StringReader(xml); 
DataSet ds = new DataSet();
ds.ReadXml(sr);

Gridview.DataSource=ds.Tables[1];
Gridview.DataBind();

Reason for doing ds.Tables[0] is that your example contains 2 sets of Answers and when you load the DataSet with your XML, the first DataTable will contain as many rows as sets of answers you have on your XML while the second table will contain all the rows from all the set of answers.

The way LinqPad displays it is as follows:

enter image description here

ACTUALIZACIÓN:

string xml=@"<?xml version=""1.0""?>
<Answers>
  <AnswerSet>
<Answer questionId=""MRN"">4444</Answer>
<Answer questionId=""FName"">test</Answer>
<Answer questionId=""LName"">patient</Answer>
<Answer questionId=""AddressPt"">blah blah</Answer>
<Answer questionId=""Governorate"">xxxx</Answer>
<Answer questionId=""InitialCSF"">Negative</Answer>
<Answer questionId=""Diagnosis""></Answer>
<Answer questionId=""Description"">
</Answer>
  </AnswerSet>
  <AnswerSet>
<Answer questionId=""MRN"">1</Answer>
<Answer questionId=""FName"">1</Answer>
<Answer questionId=""LName"">1</Answer>
<Answer questionId=""AddressPt"">1</Answer>
<Answer questionId=""InitialCSF"">Positive</Answer>
<Answer questionId=""Diagnosis"">dx</Answer>
<Answer questionId=""Description"">
</Answer>
  </AnswerSet>
 </Answers>";
StringReader sr= new StringReader(xml); 
 DataSet ds = new DataSet();
 ds.ReadXml(sr);
 ds.Merge(ds,true);
 gridview.DataSoure=ds.Tables[1];
     gridview.DataBind();

respondido 09 nov., 11:02

thanks for your answer, i am happy u tried my data. i am confused though,for example i cant find "string reader". i know its my bad: please tell me in more details - Mohamed Kamal

StringReader is in the System.IO namespace. If you do using System.IO; you should be able to create instances of this class. Our you can just fully qualify it System.IO.StringReader sr = new System.IO.StringReader(); - Ícaro

@mokokamello I have provided the code I used. Take a look at the update. - Ícaro

@mokokamello my sample code was meant to be run from a Console application, not from a Web app. I changed the code again so that you can use it on your webapp but you need to change the gridview variable name for the actual name of your gridview variable. - Ícaro

@mokokamello the XMLDataSource looks promising for what you need. There are good examples here. msdn.microsoft.com/en-us/library/13ftcwy9.aspx I honestly didn't know about it but looks like a better option. Hard to tell without actually trying it. - Ícaro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.