读XML文件
protected void Page_Load(object sender, EventArgs e)
{
XmlTextReader xmltextreader = new XmlTextReader(Server.MapPath("phonebook.xml"));
string outMsg = "";
string currentNodeName = "";
int i = 1;
//利用XmlTextReader 对象的Read()方法热循环读取也文件的节点
while (xmltextreader.Read())
{
//当节点类型是元素的时候,记录该文件的节点
if (xmltextreader.NodeType == System.Xml.XmlNodeType.Element)
currentNodeName = xmltextreader.LocalName.ToString();
if ((xmltextreader.NodeType == System.Xml.XmlNodeType.Text))
{
//把个人的属性组织成文本串,便于页面输出
outMsg += "<tr bgcolor=#fff5b1><td>" + currentNodeName + "</td><td>" + xmltextreader.Value + "</td></tr>";
}
if (xmltextreader.NodeType == System.Xml.XmlNodeType.Element && xmltextreader.LocalName.Equals("member"))
{
outMsg += "<tr><td>联络人:</td><td><font color=red>(" + (i++) + ")</font></td></tr>";
}
}
outMsg = "<table>" + outMsg + "</table>";
//关闭Xmltextreader
xmltextreader.Close();
//显示联系人的详细信息
Response.Write(outMsg);
}
写XML文件
private void SaveXMLData_nodemode()
{
try
{
//创建一个XmlDocument 对象,用于载入存储信息的XML文件
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("phonebook.xml"));
//创建一个新的member节点并将它添加到根节点下
XmlElement parentNode = xdoc.CreateElement("member");
xdoc.DocumentElement.PrependChild(parentNode);
//创建所有用于存储信息的节点
XmlElement nameNode = xdoc.CreateElement("name");
XmlElement telphoneNode = xdoc.CreateElement("telphone");
XmlElement qqNode = xdoc.CreateElement("qq");
XmlElement msnNode = xdoc.CreateElement("msn");
XmlElement cellphoneNode = xdoc.CreateElement("cellphone");
XmlElement workNode = xdoc.CreateElement("work");
XmlElement addressNode = xdoc.CreateElement("address");
XmlElement emailNode = xdoc.CreateElement("email");
//获取文本信息
XmlText nameText = xdoc.CreateTextNode(Request.Form.Get("name"));
XmlText telphoneText = xdoc.CreateTextNode(Request.Form.Get("telphone"));
XmlText qqText = xdoc.CreateTextNode(Request.Form.Get("qq"));
XmlText msnText = xdoc.CreateTextNode(Request.Form.Get("msn"));
XmlText cellphoneText = xdoc.CreateTextNode(Request.Form.Get("cellphone"));
XmlText workText = xdoc.CreateTextNode(Request.Form.Get("work"));
XmlText addressText = xdoc.CreateTextNode(Request.Form.Get("address"));
XmlText emailText = xdoc.CreateTextNode(Request.Form.Get("email"));
//将上面创建的各个存储信息的节点添加到member节点下,但并不包含最终值?
parentNode.AppendChild(nameNode);
parentNode.AppendChild(telphoneNode);
parentNode.AppendChild(qqNode);
parentNode.AppendChild(msnNode);
parentNode.AppendChild(cellphoneNode);
parentNode.AppendChild(workNode);
parentNode.AppendChild(addressNode);
parentNode.AppendChild(emailNode);
//将上面猎取的文本信息添加到与之相对应的节点中
nameNode.AppendChild(nameText);
telphoneNode.AppendChild(telphoneText);
msnNode.AppendChild(msnText);
qqNode.AppendChild(qqText);
cellphoneNode.AppendChild(cellphoneText);
workNode.AppendChild(workText);
emailNode.AppendChild(emailText);
//保存存储信息的XML文件
xdoc.Save(Server.MapPath("phonebook.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
利用XmlTextWriter写文件
private void SaveXMLData_XmlTextWriteMode()
{
try
{
FileInfo file = new FileInfo(Server.MapPath("phonebook.xml"));
if (!file.Exists)
{
XmlTextWriter textWriter = new XmlTextWriter(Server.MapPath("phonebook.xml"), System.Text.Encoding.UTF8);
textWriter.WriteStartDocument();
textWriter.WriteStartElement("phonebook");
textWriter.WriteStartElement("member");
textWriter.WriteStartElement("name", "");
textWriter.WriteString(Request.Form.Get("name"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("telphone", "");
textWriter.WriteString(Request.Form.Get("telphone"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("qq", "");
textWriter.WriteString(Request.Form.Get("qq"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("msn", "");
textWriter.WriteString(Request.Form.Get("msn"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("cellphone", "");
textWriter.WriteString(Request.Form.Get("cellphone"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("work", "");
textWriter.WriteString(Request.Form.Get("work"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("address", "");
textWriter.WriteString(Request.Form.Get("address"));
textWriter.WriteEndElement();
textWriter.WriteStartElement("email", "");
textWriter.WriteString(Request.Form.Get("email"));
textWriter.WriteEndElement();
textWriter.WriteEndDocument();
textWriter.Close();
}
else
{
SaveXMLData_nodemode();
}
Response.Redirect("viewphonebook_reader.aspx");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
查找XML文件
private void findname(string name);
{
XmlDocument xml = new XmlDocument();
bool findnode = false;
try
{
xml.Load(Server.MapPath("phonebook.xml"));
}
catch
{ }
XmlNodeReader read = new XmlNodeReader(xml);
while (read.Read())
{
if ((read.NodeType == System.Xml.XmlNodeType.Element) &&
read.LocalNmae.ToString().Equals(name))
{
read.Read();
if (read.NodeType == System.Xml.XmlNodeType.Text)
{
Response.Write(read.Value);
break;
}
}
}
}
附:pnohebook.xml
<?xml version="1.0" encoding="utf-8"?>
<phonebook>
<member>
<name>2</name>
<telphone>2121</telphone>
<qq>12</qq>
<msn>1212</msn>
<cellphone>12</cellphone>
<work>121</work>
<address />
<email>12121</email>
</member>
<member>
<name>wxy</name>
<telphone>312222</telphone>
<qq>0000</qq>
<msn>000@xxx.xxxx</msn>
<cellphone>1111111</cellphone>
<work>xxxxxxx</work>
<address>xxxxxxxxx</address>
<email>xxxx@xxx.xxx</email>
</member>
<member>
<name>sd/name>
<telphone>312222</telphone>
<qq>11286759</qq>
<msn>yishaogang@htomail.com</msn>
<cellphone>3571751</cellphone>
<work>sd/work>
<address>sdsd</address>
<email>yishaogang@yahoo.com.cn</email>
</member>
</phonebook>
protected void Page_Load(object sender,EventArgs e)
{
System.Data.DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("phonebook.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
private void writeToXml()
{
DataTable dt = new DataTable();
dt.Columns.Add("name",Type.GetType("System.String"));
dt.Columns.Add("telphone",Type.GetType("System.String"));
dt.Columns.Add("qq",Type.GetType("System.String"));
dt.Columns.Add("msn",Type.GetType("System.String"));
dt.Columns.Add("cellphone",Type.GetType("System.String"));
dt.Columns.Add("work",Type.GetType("System.String"));
dt.Columns.Add("address",Type.GetType("System.String"));
dt.Columns.Add("email",Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr[0] = "wxy";
dr[1] = "1";
dr[2] = "1";
dr[3] = "1";
dr[4] = "1";
dr[5] = "1";
dr[6] = "1";
dr[7] = "1";
dt.Rows.Add(dr);
System.Data.DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(Server.MapPath("phonebook.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}