2011-08-26 83 views
0

我有以下代码XML在ASP表中读取使用asp.net

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

我在正常的HTML表这样做,但我想在ASP表这个数据我怎么能做到这一点?

+0

如果添加一些示例Xml数据而不是用于生成Xml数据的代码,那么您的问题会更容易理解。 – Filburt

+0

当im写入xml文件它创建一个自动创建的位置,但我想读取该文件在asp:表我已阅读,但在html表中这里是我的xml写output.xml文件im给 – Mahendra

+0

Mahendra

回答

-1

您可以使用XML文件作为数据源的一个gridview为例:

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 

你检查这些文章更多的想法:

CodeProject: Reading XML Data into a DataTable Using ASP.NET

C#.net How to: Load and display XML data in ASP.net

+0

-1:这只有在XML匹配DataSet的期望时才有效。它必须是有关系的。 –

+0

不,它不需要你说的,我只是随意加载到gridview没有任何问题。不需要指定任何东西!而从他所表现出来的情况来看,他应该能够很好地工作。 – Sed