这个是添加操作:- ///创建一个XML元素
- XElement element = new XElement("DataBases",
- new XElement("DataBase",
- new XElement("id", "sya"), ///添加元素id
- new XElement("company", "上海"),
- new XElement("server","192.168.1.100"),
- new XElement("username","sa")
- )
- );
- //添加节点
- XElement ele = new XElement("DataBase",
- new XElement("id", "syabc"), ///添加元素id
- new XElement("company", "北京"),
- new XElement("server", "192.168.1.101"),
- new XElement("username", "sa123"));
- element.Add(ele);
- ///查询根元素
- IEnumerable<XElement> elements = from e in element.Elements("DataBases")
- select e;
- ///输出根元素的名称
- ///
- foreach (XElement xe in elements)
- {
- Response.Write("1" + "<br />");
- }
- ///显示XML文件的内容
- Response.Write(element);
- ///设置网页显示的形式为XML文件
- Response.ContentType = "text/xml";
- Response.End();
复制代码 |