<?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>
答案 0 :(得分:5)
我能想到将XML绑定到gridview的最简单方法是将xml加载到DataSet,然后将DataSet绑定到gridview:
StringReader sr= new StringReader(xml);
DataSet ds = new DataSet();
ds.ReadXml(sr);
Gridview.DataSource=ds.Tables[1];
Gridview.DataBind();
执行ds.Tables[0]
的原因是您的示例包含2组Answers
,当您使用XML加载DataSet时,第一个DataTable将包含与您在答案上的答案集一样多的行XML,而第二个表将包含所有答案集中的所有行。
LinqPad显示的方式如下:
<强>更新强>
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();