How to convert an XML file to a DataSource?

first, I want to report that I have never used XML as a data source before.

I have an XML file called "answers.xml" and I need to include gridview, formview, etc.

<?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> 

I need a way to change data (edit / delete) in an XML file using .NET

+4
source share
2 answers

If you do not want to use the XmlDataSource control, you can also use LINQ to XML to create a data source object from a file:

 XDocument doc = XDocument.Load("somefile.xml"); var results = from answer in doc.Descendants("Answer") select new { Question = answer.Attribute("questionId").Value, Answer = answer.Value }; GridView1.DataSource = results; GridView1.DataBind(); 
+3
source

The System.Web.UI.WebControls.XmlDataSource class returns an ASP.NET data object with a given XML file. This link contains sample code and a walkthrough.

Note that if you must use XML attributes as keys, your XPath expressin will contain things like Answer[@MRN] .

+2
source

Source: https://habr.com/ru/post/1380470/


All Articles