asp.net,C#如何操作SqlServer中的XML类型数据?
现在我是在SqlServer数据库中有一个字段是XML类型,然后现在需要把XML这个类型的数据读出去,可是怎么操作啊?以前只用C#操作过XML文件,那个是有相关的几个对象...
现在我是在SqlServer数据库中有一个字段是XML类型,然后现在需要把XML这个类型的数据读出去,可是怎么操作啊?以前只用C#操作过XML文件,那个是有相关的几个对象的,用它们就可以操作XML文件了。可是XML字段如何操作得出里面的元素和属性???
展开
1个回答
2013-06-01
展开全部
从xml文件中读出数据,然后绑定到gridview里面就好了啊给你段代码,希望对你有所帮助using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;public partial class XML_SQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
//private string str = "server=97514BD2A5C9474;database=a;uid=sa;pwd=;";
//private XmlDataDocument doc;
protected void Button1_Click(object sender, EventArgs e)
{
//XmlDataDocument doc;
//SqlConnection con = new SqlConnection(str);
//con.Open();
//SqlDataAdapter da = new SqlDataAdapter("select * from 结果", con);
DataSet ds = new DataSet();
ds.ReadXml("c:\\sample.xml");
//da.Fill(ds, "结果");
//ds.WriteXml("c:\\sample.xml", XmlWriteMode.WriteSchema); dg.DataSource = ds;
//doc = new XmlDataDocument(ds);
//XmlNodeList xlist = doc.GetElementsByTagName("结果");
dg.DataBind();
TextBox1.Text = "";
foreach (DataTable dt in ds.Tables)
{
TextBox1.Text += dt.TableName + "\r\n";
foreach (DataColumn col in dt.Columns)
{
TextBox1.Text += "\t" + col.ColumnName + "-" + col.DataType.FullName + "\r\n";
}
}
}
}
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;public partial class XML_SQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
//private string str = "server=97514BD2A5C9474;database=a;uid=sa;pwd=;";
//private XmlDataDocument doc;
protected void Button1_Click(object sender, EventArgs e)
{
//XmlDataDocument doc;
//SqlConnection con = new SqlConnection(str);
//con.Open();
//SqlDataAdapter da = new SqlDataAdapter("select * from 结果", con);
DataSet ds = new DataSet();
ds.ReadXml("c:\\sample.xml");
//da.Fill(ds, "结果");
//ds.WriteXml("c:\\sample.xml", XmlWriteMode.WriteSchema); dg.DataSource = ds;
//doc = new XmlDataDocument(ds);
//XmlNodeList xlist = doc.GetElementsByTagName("结果");
dg.DataBind();
TextBox1.Text = "";
foreach (DataTable dt in ds.Tables)
{
TextBox1.Text += dt.TableName + "\r\n";
foreach (DataColumn col in dt.Columns)
{
TextBox1.Text += "\t" + col.ColumnName + "-" + col.DataType.FullName + "\r\n";
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |