读取XML内容
XmlDocument xml=new XmlDocument();
xml.Load(Server.MapPath("text.xml"));
XmlNode node = xml.SelectSingleNode("root");//根节点
XmlNode nd = node.SelectSingleNode("city");//根据根节点获取子节点
TextBox1.Text = nd.InnerText;//读出节点“city”的内容
XmlNodeList nodelist = xml.SelectNodes("root");
//遍历所有节点的内容
foreach (XmlNode node1 in nodelist)
{
ListBox1.Items.Add(node1.InnerText);
}
更新XML文件
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("text.xml"));
XmlNode node = xml.SelectSingleNode("root");//根节点
XmlNode nd = node.SelectSingleNode("city");//根据根节点获取子节点
TextBox1.Text = nd.InnerText;//读出节点“city”的内容
nd.InnerText = "胡楼";
xml.Save(Server.MapPath("text.xml"));
创建XML文件
XmlDocument xml = new XmlDocument();
XmlNode node = xml.CreateXmlDeclaration("1.0","big5","");
xml.AppendChild(node);
//新建根节点
XmlNode root = xml.CreateElement("rui");
xml.AppendChild(root);
XmlNode a=xml.CreateNode(XmlNodeType.Element,"city","");
a.InnerText = "深圳";
root.AppendChild(a);
XmlNode b=xml.CreateNode(XmlNodeType.Element,"address","");
b.InnerText = "MSI";
root.AppendChild(b);
XmlNode c = xml.CreateNode(XmlNodeType.Element,"time","");
c.InnerText = "8:30";
root.AppendChild(c);
//新建一个父节点
XmlNode item = xml.CreateNode(XmlNodeType.Element,"item","");
root.AppendChild(item);//追加到根节点
XmlNode a1 = xml.CreateNode(XmlNodeType.Element,"city","");
a1.InnerText = "郑州";
item.AppendChild(a1);
XmlNode b1 = xml.CreateNode(XmlNodeType.Element,"adderss","");
b1.InnerText = "郑州大学";
item.AppendChild(b1);
XmlNode c1 = xml.CreateNode(XmlNodeType.Element,"time","");
c1.InnerText = "8:30";
item.AppendChild(c1);
xml.Save(Server.MapPath("zrh.xml"));