Click here to Skip to main content
16,003,555 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 13-May-16 4:02am
v2

C#
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.
 
Share this answer
 
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.
C#
string createXMLpath = @"C:\\Temp\\myXMLfile.xml";

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

C#
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();
              }
          }

C#
ds.ReadXml(xmlResult);
       GridView1.DataSource = ds;
       GridView1.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900