如何将XML文件转换为DataSource?

时间:2021-08-17 06:20:09

first i want to let you know that i never used XML as datasource before.

首先,我想让您知道我之前从未使用过XML作为数据源。

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

我有一个名为“answers.xml”的XML文件,我需要连接gridview,formview等...

<?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 the data (edit / delete) in the xml file using .NET

我需要一种方法来使用.NET更改xml文件中的数据(编辑/删除)

2 个解决方案

#1


3  

If you don't want to use an XmlDataSource control, you could also use LINQ to XML to create a datasource object from the file:

如果您不想使用XmlDataSource控件,还可以使用LINQ to XML从该文件创建数据源对象:

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();

#2


2  

The System.Web.UI.WebControls.XmlDataSource class will return an ASP.NET DataSource given an XML file. The given link includes sample code and a walkthrough.

System.Web.UI.WebControls.XmlDataSource类将返回给定XML文件的ASP.NET DataSource。给定的链接包括示例代码和演练。

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

请注意,如果必须使用XML属性作为键,则XPath expressin将包含Answer [@MRN]之类的内容。

#1


3  

If you don't want to use an XmlDataSource control, you could also use LINQ to XML to create a datasource object from the file:

如果您不想使用XmlDataSource控件,还可以使用LINQ to XML从该文件创建数据源对象:

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();

#2


2  

The System.Web.UI.WebControls.XmlDataSource class will return an ASP.NET DataSource given an XML file. The given link includes sample code and a walkthrough.

System.Web.UI.WebControls.XmlDataSource类将返回给定XML文件的ASP.NET DataSource。给定的链接包括示例代码和演练。

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

请注意,如果必须使用XML属性作为键,则XPath expressin将包含Answer [@MRN]之类的内容。