写XML文档
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Text;
public partial class _20110327XML_XMLWriter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string StrXml
{
get {
return Convert.ToString(ViewState["StrXml"]);
}
set {
ViewState["StrXml"] = value;
}
}
protected void btnXMLWriter_Click(object sender, EventArgs e)
{ //生成XmlTextWriter类的实例
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("book.xml"), Encoding.UTF8);
writer.WriteStartDocument(); //开始XML文档写入
writer.WriteStartElement("BOOKS"); //开始写<BOOKS>元素
writer.WriteStartElement("BOOK"); //开始写<BOOK>元素
//下面开始写<BOOK>元素里面的底层元素
if (this.title.Text == "")
{
writer.WriteElementString("Title", "none");
}
else
{
writer.WriteElementString("Title", this.title.Text.Trim());
}
if (this.author.Text == "")
{
writer.WriteElementString("Title", "none");
}
else
{
writer.WriteElementString("Title", this.author.Text.Trim());
}
if (this.press.Text == "")
{
writer.WriteElementString("Title", "none");
}
else
{
writer.WriteElementString("Title", this.press.Text.Trim());
}
if (this.pages.Text == "")
{
writer.WriteElementString("Title", "none");
}
else
{
writer.WriteElementString("Title", this.pages.Text.Trim());
}
if (this.price.Text == "")
{
writer.WriteElementString("Title", "none");
}
else
{
writer.WriteElementString("Title", this.price.Text.Trim());
}
writer.WriteEndElement(); //结束<BOOK>元素
writer.WriteStartElement("AuthorInfo"); //开始写<AuthorInfo>元素
writer.WriteAttributeString("age", "40");
writer.WriteElementString("Address", "浙江杭州");
writer.WriteElementString("Postalcode", "310015");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
Response.Redirect("book.xml");
}
}
读XML文档
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Xml;
public partial class _20110327XML_XMLReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnXMLReader_Click(object sender, EventArgs e)
{
string url = "D:\\xiongzaiqiren\\Visual Studio 2008\\Projects\\XXH_LogManager\\XXH_LogManager\\20110327XML\\book.xml";
XmlTextReader reader = new XmlTextReader(url);
while (reader.Read())
{
if (reader.HasAttributes)
{
Response.Write("数据段为:" + reader.NodeType.ToString() + ";" + reader.Name + ";" + reader.Value + "<br />");
Response.Write("该数据段的属性有:");
for (int i = 0; i < reader.AttributeCount - 1; i++)
{
reader.MoveToAttribute(i);
Response.Write(reader.GetAttribute(i) + ";");
}
Response.Write("<br />");
reader.MoveToElement();
}
reader.MoveToElement();
}
reader.Close();
}
//以下是转化XML方法
#region 获得Xml文档对象的方法
/// <summary>
/// 获得Xml文档对象的方法
/// </summary>
/// <param name="fullFileName">XML完整文件名</param>
/// <returns>Xml文档对象</returns>
public static XmlDocument GetXmlDocument(string fullFileName)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(fullFileName);
return doc;
}
catch
{
throw new Exception("XML文件" + fullFileName + "不存在或拒绝访问。");
}
}
/// <summary>
/// 将XmlDocument转化为DataSet
/// </summary>
/// <param name="doc">xml文档对象</param>
/// <returns></returns>
public static DataSet GetDataSetByXmlDocument(XmlDocument doc)
{
XmlNodeReader reader = new XmlNodeReader(doc);
DataSet ds = new DataSet();
ds.ReadXml(reader);
reader.Close();
return ((ds != null) && (ds.Tables[0].Rows.Count > 0)) ? ds : null;
}
#endregion
#region 根据标记名称获得数据集合的方法
/// <summary>
/// 根据标记名称获得数据集合的方法
/// </summary>
/// <param name="fileName">XML完整文件名</param>
/// <param name="tagName">标记名称</param>
/// <returns>数据集合</returns>
public static ArrayList GetArrayListByTagName(string fileName, string tagName)
{
XmlDocument doc = GetXmlDocument(fileName);
XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName(tagName);
ArrayList al = new ArrayList();
for (int i = 0; i < nodeList.Count; i++)
{
al.Add(nodeList[i].InnerText);
}
return al;
}
#endregion
}
关于XML转换数据集XMLHelper.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
namespace KangHui.Common
{
/// <summary>
/// 对XML文件进行处理的类
/// </summary>
public class XmlHelper
{
#region 获得Xml文档对象的方法
/// <summary>
/// 获得Xml文档对象的方法
/// </summary>
/// <param name="fullFileName">XML完整文件名</param>
/// <returns>Xml文档对象</returns>
public static XmlDocument GetXmlDocument(string fullFileName)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(fullFileName);
return doc;
}
catch
{
throw new Exception("XML文件" + fullFileName + "不存在或拒绝访问。");
}
}
#endregion
#region 根据标记名称获得数据集合的方法
/// <summary>
/// 根据标记名称获得数据集合的方法
/// </summary>
/// <param name="fileName">XML完整文件名</param>
/// <param name="tagName">标记名称</param>
/// <returns>数据集合</returns>
public static ArrayList GetArrayListByTagName(string fileName, string tagName)
{
XmlDocument doc = GetXmlDocument(fileName);
XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName(tagName);
ArrayList al = new ArrayList();
for (int i = 0; i < nodeList.Count; i++)
{
al.Add(nodeList[i].InnerText);
}
return al;
}
/// <summary>
/// 将XmlDocument转化为Dataset
/// </summary>
/// <param name="doc">xml文档对象</param>
/// <returns></returns>
public static DataSet GetDataSetByXmlDocument(XmlDocument doc)
{
XmlNodeReader reader = new XmlNodeReader(doc);
DataSet ds = new DataSet();
ds.ReadXml(reader);
reader.Close();
return ((ds != null) && (ds.Tables[0].Rows.Count > 0)) ? ds : null;
}
#endregion
}
}