asp.net操作xml
1.xml文档Products.xml
<?xml version="1.0" encoding="utf-8"?>
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.pro.org/2001/products" xsi:schemaLocation="http://www.pro.org/2001/products products.xsd">
<item belong="数码">
<id>1</id>
<name>手机</name>
<price>1000</price>
</item>
<item belong="服装">
<id>2</id>
<name>男装</name>
<price>200</price>
</item>
<item belong="食品">
<id>3</id>
<name>黄瓜</name>
<price>4</price>
</item>
</products>
2.schema约束文档 products.xml
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pro.org/2001/products" xmlns:pro="http://www.pro.org/2001/products" elementFormDefault="qualified">
<element name="products" type="pro:pro"></element>
<complexType name="pro">
<sequence>
<element name="item" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="id" type="string"></element>
<element name="name" type="string"></element>
<element name="price">
<simpleType>
<restriction base="float">
<maxExclusive value="10000"></maxExclusive>
<minInclusive value="0"></minInclusive>
</restriction>
</simpleType>
</element>
</sequence>
<attribute name="belong" type="string"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</schema>
3.定义实体类 DBPro.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///DBPro 的摘要说明
/// </summary>
public class DBPro
{
string belong;
string id;
string name;
string price;
public DBPro(string belong,string id,string name,string price)
{
this.belong = belong;
this.id = id;
this.name = name;
this.price = price;
}
public string Belong
{
get { return belong; }
set { belong = value; }
}
public string ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set{name=value;}
}
public string Price
{
get { return price; }
set { price = value; }
}
}
4.新建一个web窗体Defaut.aspx,在Default.aspx.cs中编写核心代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ //选择方法进行测试
//SearchXml();
DBPro pro = new DBPro("家电","10", "电视", "3999");
AddToXml(pro);
//UpdateOneXml(pro);
//DeleteXml("10");
}
/// <summary>
/// 遍历xml文档
/// </summary>
/// <param name="pro"></param>
private void SearchXml()
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//获取节点列表
XmlNodeList items = root.ChildNodes;
//遍历item项
Response.Write("<pre>");
foreach (XmlNode item in items)
{
//输出属性
Response.Write(item.Attributes["belong"].Name + "=" + item.Attributes["belong"].InnerText);
//遍历输出子节点
foreach (XmlNode p in item)
{
Response.Write(p.Name + "=" + p.InnerText);
}
}
Response.Write("</pre>");
}
/// <summary>
/// xml添加
/// </summary>
/// <param name="pro"></param>
private void AddToXml(DBPro pro)
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//创建元素
XmlElement newItem = xd.CreateElement("item");
XmlElement newID = xd.CreateElement("id");
XmlElement newName = xd.CreateElement("name");
XmlElement newPrice = xd.CreateElement("price");
//配置参数
newItem.SetAttribute("belong", pro.Belong);
newID.InnerText = pro.ID;
newName.InnerText = pro.Name;
newPrice.InnerText = pro.Price;
//装配
root.AppendChild(newItem);
newItem.AppendChild(newID);
newItem.AppendChild(newName);
newItem.AppendChild(newPrice);
xd.Save(System.Web.HttpContext.Current.Server.MapPath("Products.xml"));
}
/// <summary>
/// 修改xml一项
/// </summary>
/// <param name="pro"></param>
private void UpdateOneXml(DBPro pro)
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//获取节点列表
XmlNodeList items = root.ChildNodes;
//遍历节点列表
foreach (XmlNode item in items)
{
//遍历item
foreach (XmlNode p in item)
{
if (p.Name == "id" && p.InnerText == pro.ID)
{
item.Attributes["belong"].InnerText = pro.Belong;
p.NextSibling.InnerText = pro.Name;
p.NextSibling.NextSibling.InnerText = pro.Price;
}
}
}
}
/// <summary>
/// 删除xml一项
/// </summary>
/// <param name="pro"></param>
private void DeleteXml(string id)
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(System.Web.HttpContext.Current.Server.MapPath("Products.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//获取节点列表
XmlNodeList items = root.ChildNodes;
//遍历节点列表
foreach (XmlNode item in items)
{
//遍历item
foreach (XmlNode p in item)
{
if (p.Name == "id" && p.InnerText == id)
{
root.RemoveChild(item);
}
}
}
}
}
此处应注意:用XMLDocument添加元素,遇到了这样一个问题:
当根节点具有 xmlns 属性时,用 XMLDocument 创建子元素时如果不指定 xmlns 或指定 xmlns 为 null 时,子元素将自动具有 xmlns="" 属性
<item belong="家电" xmlns="">
<id>10</id>
<name>电视</name>
<price>3999</price>
</item>
问题原因:
当父节点具有 xmlns 属性时,子节点必须指定 xmlns 属性,仅当子节点的 xmnls 属性与父节点相同时,子节点才不显示 xmlns 属性,最终就不会在 .xml 文件中显示出来
解决办法:
XmlElement newItem = xd.CreateElement("item",xd.DocumentElement.NamespaceURI);
XmlElement newID = xd.CreateElement("id",xd.DocumentElement.NamespaceURI);
XmlElement newName = xd.CreateElement("name",xd.DocumentElement.NamespaceURI);
XmlElement newNumber = xd.CreateElement("number",xd.DocumentElement.NamespaceURI);
在每一个下级节点,都要继续指定命名空间,否则仍会出现 xmlns="" 属性。
///新手上道,若有不足或错误之处,敬请各位大神批评指正!