Read and Write XML
2007-03-13 15:01
369 查看
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
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.IO; //this
using System.Text; //this
using System.Xml; //this
/// <summary>
/// 生成XML文件
/// </summary>
private void writeXml()
{
try
{
string strName = Guid.NewGuid().ToString().Replace("-","");
Session["filename"] = "Read" + strName + ".xml";
string sFilePath = Server.MapPath("../TempFiles/" + Session["filename"].ToString());
XmlTextWriter writer = new XmlTextWriter(sFilePath, Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement("INPUT");
writer.WriteStartElement("TextBox");
foreach (Control ctl in form1.Controls)
{
if (ctl.GetType().Name == "TextBox")
{
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctl.ID);
writer.WriteElementString(tb.ID, tb.Text);
}
else if (ctl.GetType().Name == "DropDownList")
{
DropDownList tb = new DropDownList();
tb = (DropDownList)this.FindControl(ctl.ID);
writer.WriteElementString(tb.ID, tb.SelectedItem.Text);
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void loadXml(string strFileName)
{
DataSet mydds = new DataSet();
string sFilePath = Server.MapPath("../TempFiles/" + strFileName);
//Session.Remove("filename");
if (File.Exists(sFilePath))
{
mydds.ReadXml(sFilePath);
for (int i = 0; i < mydds.Tables[0].Columns.Count; i++)
{
try
{
TextBox tbx = new TextBox();
tbx = (TextBox)this.FindControl(mydds.Tables[0].Columns[i].ColumnName);
tbx.Text = mydds.Tables[0].Rows[0][i] + "";
}
catch (Exception ex)
{
throw ex;
}
}
}
else
{
Session.Remove("filename");
throw new Exception("Custm");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
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.IO; //this
using System.Text; //this
using System.Xml; //this
/// <summary>
/// 生成XML文件
/// </summary>
private void writeXml()
{
try
{
string strName = Guid.NewGuid().ToString().Replace("-","");
Session["filename"] = "Read" + strName + ".xml";
string sFilePath = Server.MapPath("../TempFiles/" + Session["filename"].ToString());
XmlTextWriter writer = new XmlTextWriter(sFilePath, Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement("INPUT");
writer.WriteStartElement("TextBox");
foreach (Control ctl in form1.Controls)
{
if (ctl.GetType().Name == "TextBox")
{
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctl.ID);
writer.WriteElementString(tb.ID, tb.Text);
}
else if (ctl.GetType().Name == "DropDownList")
{
DropDownList tb = new DropDownList();
tb = (DropDownList)this.FindControl(ctl.ID);
writer.WriteElementString(tb.ID, tb.SelectedItem.Text);
}
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void loadXml(string strFileName)
{
DataSet mydds = new DataSet();
string sFilePath = Server.MapPath("../TempFiles/" + strFileName);
//Session.Remove("filename");
if (File.Exists(sFilePath))
{
mydds.ReadXml(sFilePath);
for (int i = 0; i < mydds.Tables[0].Columns.Count; i++)
{
try
{
TextBox tbx = new TextBox();
tbx = (TextBox)this.FindControl(mydds.Tables[0].Columns[i].ColumnName);
tbx.Text = mydds.Tables[0].Rows[0][i] + "";
}
catch (Exception ex)
{
throw ex;
}
}
}
else
{
Session.Remove("filename");
throw new Exception("Custm");
}
}
相关文章推荐
- Qt Write and Read XML File 读写XML文件
- Qt Write and Read XML File 读写XML文件
- How To Read and Write XML Documents with GDataXML(使用GData读写XML文档)
- How To Read and Write XML Documents with GDataXML
- csharp read execl and write xml
- opencv xml read and write
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
- read and write binary xml
- How To Read and Write XML Documents with GDataXML
- How To Read and Write XML Documents with GDataXML
- Infopath XML read and write
- write to and read from files (读写文件)
- XmlWriteMode和XmlReadMode的作用是什么?
- Read ALAsset movie file and write into the disk
- How to mount partition with ntfs file system and read write access
- Readonly and Mutable, vs. Read / Write Properties
- Freemaker的java.beans.IntrospectionException: type mismatch between read and write methods
- Read and Write files IN PHP
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- read and write '.bmp' file