如何在ASP.NET中将XML字符串作为gridview的数据源 [英] How do I make XML string as a datasource for gridview in ASP.NET

查看:59
本文介绍了如何在ASP.NET中将XML字符串作为gridview的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串变量中有xml。如何将此字符串作为gridview的数据源



请指导我。



我尝试过:



保存到xml文件但无法读取。

I have xml in string variable. how can i make this string as a datasource for gridview

Please guide me.

What I have tried:

Save in to xml file but not able to read.

推荐答案

You can use below code to convert xmlstring to Datset and then bind the gridView.

You can use a StringReader to load it into a DataSet. From there, the table with the first index will contain the DataTable.

public DataTable stam()    
{
    StringReader theReader = new StringReader(xmlDataString);
    DataSet theDataSet = new DataSet();
    theDataSet.ReadXml(theReader);

    return theDataSet.Tables[0];
}

Above code will work fine if your XMlString is valid XMLString.


这里我正在创建xml到文件中。它将通过代码创建并检查文件的可用性(如果存在)它将删除xml文件的旧包含并将新的xml保存到此文件中。

Here i'm creating xml into file. it'll create by code and check availability of the file if exist it will delete old contain of the xml file and saves the new xml into this.
string createXMLpath = @"C:\\Temp\\myXMLfile.xml";




string xmlResult = xDocc.Descendants("RootResults").DescendantNodes().First().ToString();




if (!File.Exists(createXMLpath))
          {
              File.Create(createXMLpath).Close();
              File.WriteAllText(createXMLpath, xmlResult);
          }
           else if (File.Exists(createXMLpath))
          {
              File.WriteAllText(createXMLpath, String.Empty);
              using (StreamWriter sw = new StreamWriter(createXMLpath, true))
              {
                  sw.WriteLine(xmlResult);
                  sw.Close();
              }
          }




ds.ReadXml(xmlResult);
       GridView1.DataSource = ds;
       GridView1.DataBind();


这篇关于如何在ASP.NET中将XML字符串作为gridview的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆