C# 使用asp.net在asp表中读取xml

C# 使用asp.net在asp表中读取xml,c#,asp.net,C#,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")

我有以下代码

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);
    }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
//本部分用于编写xml文件
//字符串文件输入代码此处name=“E:\\application server\\dotnetrnd\\mahendra\\my.xml”;
字符串文件名=Server.MapPath(“\\my.xml”);
XmlDocument doc=新的XmlDocument();
XmlTextWriter xmlWriter=新的XmlTextWriter(文件名,System.Text.Encoding.UTF8);
xmlWriter.Formatting=Formatting.Indented;
WriteProcessingInstruction(“xml”,“版本='1.0'编码='UTF-8'”);
writeStarteElement(“StateDetail”);
xmlWriter.Close();
doc.Load(文件名);
con.Open();
cmd=新的SqlCommand(“状态更新”,con);
cmd.CommandType=CommandType.storedProcess;
dr=cmd.ExecuteReader();
尝试
{
如果(哈斯罗博士)
{
while(dr.Read())
{
XmlNode root=doc.DocumentElement;
XmlElement child1=doc.CreateElement(“状态”);
//检索状态ID
XmlAttribute ID1=doc.CreateAttribute(“id”);
ID1.Value=dr[“id_State”].ToString();
child1.Attributes.Append(ID1);//在chil1中添加属性
//检索州名
XmlAttribute名称1=doc.CreateAttribute(“名称”);
name1.Value=dr[“State_name”].ToString();
child1.Attributes.Append(name1);
//检索状态描述
xmldattribute description=doc.CreateAttribute(“description”);
description.Value=dr[“State_description”].ToString();
child1.Attributes.Append(description);
//添加到根元素中
根。追加子(child1);
//Response.Write(“

”+dr[“State_name”].ToString()); //在child1中创建子元素 con1.Open(); cmd1=新的SqlCommand(“XMLCity\u retrve”,con1); 参数.Add(“@id_State”,SqlDbType.VarChar).Value=dr[“id_State”].ToString(); cmd1.CommandType=CommandType.StoredProcess; dr2=cmd1.ExecuteReader(); if(dr2.HasRows) { while(dr2.Read()) { //响应。写入(“
”+dr2[“城市名称”].ToString()); //检索城市ID xmlement city=doc.CreateElement(“城市”); XmlAttribute ID2=doc.CreateAttribute(“id”); ID2.Value=dr2[“id_City”].ToString(); city.Attributes.Append(ID2);//在chil1中添加属性 //检索城市名称 xmldattribute name2=doc.CreateAttribute(“名称”); name2.Value=dr2[“城市名称”].ToString(); city.Attributes.Append(name2); //检索城市描述 xmldattribute descr=doc.CreateAttribute(“说明”); descr.Value=dr2[“城市描述”].ToString(); city.Attributes.Append(descr); //添加到child1元素中 儿童1.儿童(城市); } } dr2.Close(); con1.Close(); } } 文件保存(文件名); Close博士(); con.Close(); } 捕获(例外情况除外) { 响应。写入(“异常为”+ex.Message); } //这一部分用于读取xml 尝试 { 数据表dt; 使用(XmlReader=XmlReader.Create(文件名)) { 字符串结果; while(reader.Read()) { if(reader.NodeType==XmlNodeType.Element) { 结果=”;
//对于(int count=1;count,您可以将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
您可以查看这些文章以了解更多想法:


如果您添加一些示例Xml数据,而不是生成Xml数据所用的代码,您的问题将更容易理解。当im编写Xml文件时,它会在位置上自动创建一个文件,但我想在asp:table中读取该文件,我已经读过了,但在html表中这是我的Xml writing output.Xml文件,我给出了-1因为这段代码有很多错误。我不想让任何人认为它是好的代码来复制。-1:这只在XML符合
数据集
的期望时起作用。它必须是某种关系型的。不,它不需要你说的,我只是将一个随机XML加载到gridview中,没有任何问题。不需要指定任何内容g!!!从他的表现来看,这对他来说应该很好。