I have the following code
protected void Page_Load(object sender, EventArgs e)
{
//this part use to writing the xml file
//string filenenter code here name = "E:\\application server\\dotnetrnd\\mahendra\\my.xml";
string filename = Server.MapPath("\\my.xml");
XmlDocument doc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("StateDetail");
xmlWriter.Close();
doc.Load(filename);
con.Open();
cmd = new SqlCommand("State_Update", con);
cmd.CommandType = CommandType.StoredProcedure;
dr = cmd.ExecuteReader();
try
{
if (dr.HasRows)
{
while (dr.Read())
{
XmlNode root = doc.DocumentElement;
XmlElement child1 = doc.CreateElement("State");
// Retriving State ID
XmlAttribute ID1 = doc.CreateAttribute("id");
ID1.Value = dr["id_State"].ToString();
child1.Attributes.Append(ID1); //Adding attribute in chil1
// Retriving State Name
XmlAttribute name1 = doc.CreateAttribute("name");
name1.Value = dr["State_name"].ToString();
child1.Attributes.Append(name1);
// Retriving State Description
XmlAttribute description = doc.CreateAttribute("description");
description.Value = dr["State_description"].ToString();
child1.Attributes.Append(description);
//Adding into root element
root.AppendChild(child1);
//Response.Write("<br><br>" + dr["State_name"].ToString());
//Creating sub-element in child1
con1.Open();
cmd1 = new SqlCommand("XMLCity_Retrive", con1);
cmd1.Parameters.Add("@id_State", SqlDbType.VarChar).Value = dr["id_State"].ToString();
cmd1.CommandType = CommandType.StoredProcedure;
dr2 = cmd1.ExecuteReader();
if (dr2.HasRows)
{
while (dr2.Read())
{
// Response.Write("<br>"+dr2["City_name"].ToString());
// Retriving City ID
XmlElement city = doc.CreateElement("City");
XmlAttribute ID2 = doc.CreateAttribute("id");
ID2.Value = dr2["id_City"].ToString();
city.Attributes.Append(ID2); //Adding attribute in chil1
// Retriving City Name
XmlAttribute name2 = doc.CreateAttribute("name");
name2.Value = dr2["City_name"].ToString();
开发者_如何学运维 city.Attributes.Append(name2);
// Retriving City Description
XmlAttribute descr = doc.CreateAttribute("description");
descr.Value = dr2["City_description"].ToString();
city.Attributes.Append(descr);
//Adding into child1 element
child1.AppendChild(city);
}
}
dr2.Close();
con1.Close();
}
}
doc.Save(filename);
dr.Close();
con.Close();
}
catch (Exception ex)
{
Response.Write("Exception is " + ex.Message);
}
//this part use for reading xml
try
{
DataTable dt;
using (XmlReader reader = XmlReader.Create(filename))
{
string result;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
result = "";
//for (int count = 1; count <= reader.Depth; count++)
// {
result += "==";
//}
//result += "=>" + reader.Name;
Response.Write(" " + reader.Value);
//tr2 = new TableRow();
// td2 = new TableCell();
//lblres.Text += result;
//Response.Write(result);
if (reader.HasAttributes)
{
//lblres.Text += "(";
// for (int count = 0; count < reader.AttributeCount; count++)
//{
//reader.MoveToAttribute(count);
// reader.MoveToFirstAttribute();
// reader.MoveToNextAttribute();
//lblres.Text += " " + reader.GetAttribute(0).ToString().Trim();
//lblres.Text += " " + reader.GetAttribute(1);
// lblres.Text += " " + reader.GetAttribute(2);
Table tb = new Table();
TableRow tr = null;
TableCell td = null;
tr = new TableRow();
td = new TableCell();
td.Text = reader.GetAttribute(0).ToString();
td.BorderStyle = BorderStyle.Ridge;
tr.Cells.Add(td);
t1.Rows.Add(tr);
td = new TableCell();
td.Text = reader.GetAttribute(1).ToString();
td.BorderStyle = BorderStyle.Ridge;
tr.Cells.Add(td);
td = new TableCell();
td.Text = reader.GetAttribute(2).ToString();
td.BorderStyle = BorderStyle.Ridge;
tr.Cells.Add(td);
t1.Rows.Add(tr);
// Response.Write(" " + reader.GetAttribute(0).ToString().Trim() + "<br>");
// Response.Write(" " + reader.GetAttribute(1).ToString().Trim());
// Response.Write(" " + reader.GetAttribute(2).ToString().Trim() );
//Response.Write("" + reader.GetAttribute(0));
// }
// lblres.Text = ")";
reader.MoveToElement();
}
//lblres.Text += "<br>";
Response.Write("<br>");
}
}
}
DataSet XMLDataset;
string filepath = Server.MapPath("my.xml");
XMLDataset = new DataSet();
XMLDataset.ReadXml(filepath);
gridxml.DataSource = XMLDataset.Tables["State"];
XMLDataset.Clone();
gridxml.DataBind();
}
catch (Exception exp)
{
Response.Write("An exception occured : " + exp.Message);
}
}
I have done this in normal HTML table but I want this data in Asp table how can I do this?
You can use your xml file as a data source for a gridview for an example:
DataSet dataSet = new DataSet(); //create an empty dataset
dataSet.ReadXml(@"C:\somewhere\file.xml"); //fill it with xml file content
GridView1.DataSource = dataSet.Tables[0]; //first table from dataset to be loaded
GridView1.DataBind(); //load the data into gridview
And you check these articles for more ideas:
CodeProject: Reading XML Data into a DataTable Using ASP.NET
C#.net How to: Load and display XML data in ASP.net
精彩评论