日期:2014-05-18 浏览次数:20434 次
private string strconn="Provider=SQLOLEDB.1;User ID=sa;password=;Initial Catalog=通信录;Data Source=lhz"; public OleDbConnection cn = new OleDbConnection(); public OleDbCommand cm = new OleDbCommand() ; private OleDbDataAdapter da = new OleDbDataAdapter(); Xml XMLFile1=new Xml(); cn.ConnectionString=strconn; cm.Connection=cn; cm.CommandText="select CustID,CustName,QQ,Email from tongxin"; da.SelectCommand=cm; DataSet ds=new DataSet(); da.Fill(ds,"tongxin"); try { ds.WriteXml(Request.MapPath(XMLFile1.xml)); } catch(Exception) { } finally { DataGrid1.DataSource=ds; DataGrid1.DataBind(); }
------解决方案--------------------
将数据读取到dataset里
然后循环插入XML
------解决方案--------------------
xmltextwriter,可以writestartdoc,writesttartelement,writevalue,
先写跟节点,然后写子节点,然后关闭写子节点,关闭写根节点
------解决方案--------------------
//声明一个XmlDocument空对象
protected XmlDocument XmlDoc = new XmlDocument();
/// <summary>
/// 构造函数,导入Xml文件
/// </summary>
/// <param name="xmlFile">文件虚拟路径</param>
public XmlOp(string xmlFile)
{
try
{
XmlDoc.Load(GetXmlFilePath(xmlFile)); //载入Xml文档
}
catch (System.Exception ex)
{
throw ex;
}
}
/// <summary>
/// 析构函数
/// </summary>
~XmlOp()
{
XmlDoc = null; //释放XmlDocument对象
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="filePath">文件虚拟路径</param>
public void Save(string filePath)
{
try
{
XmlDoc.Save(GetXmlFilePath(filePath));
}
catch (System.Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回Xml文件实际路径
/// </summary>
/// <param name="xmlFile">文件虚拟路径</param>
/// <returns></returns>
public string GetXmlFilePath(string xmlFile)
{
return HttpContext.Current.Server.MapPath(xmlFile);
}
/// <summary>
/// 根据Xml文件的节点路径,返回一个DataSet数据集
/// </summary>
/// <param name="XmlPathNode">Xml文件的某个节点</param>
/// <returns></returns>
public DataSet GetDs(string XmlPathNode)
{
DataSet ds = new DataSet();
try
{