C#操作XML数据常用方法
3,/Vote.xml
复制代码
复制代码
4,/App_Code/VoteInfo.cs
复制代码
using System;
///
///Vote 的摘要说明
///
public class VoteInfo
{
// 1, Attributes
///
/// 图书名称
///
string _belong;
///
/// 编号
///
string _id;
///
/// 作者
///
string _name;
///
/// 书本数量
///
string _number;
// 2, Struct
public VoteInfo(string belong, string id,string name, string number) {
this._belong = belong;
this._id = id;
this._name = name;
this._number = number;
}
public VoteInfo()
{
}
//封装字段
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 Number
{
get { return _number; }
set { _number = value; }
}
}
复制代码
5,/DemoXml.aspx.cs ylb_tip: 这儿是.net对Xml操作的核心代码区,请认真看,一定要把PPT课件看完,对根节点和节点要理解透
复制代码
using System;
using System.Xml;
public partial class DemoXML : System.Web.UI.Page
{
///
/// ylb:1, 遍历xml文档
///
private void BianLi()
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("Vote.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//获取节点列表
XmlNodeList items = root.ChildNodes;
//遍历item项
Response.Write("
");");foreach (XmlNode item in items)
{
//输出属性
Response.Write(item.Attributes["belong"].Name+"="+item.Attributes["belong"].InnerText+"\t");
//输出子节点
foreach (XmlNode p in item)
{
Response.Write(https://www.360docs.net/doc/f84162051.html,+"="+p.InnerText+"\t");
}
Response.Write("\n");
}
Response.Write("
}
///
/// ylb:2, 添加
///
///
private void Add(VoteInfo item)
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("Vote.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//创建元素
XmlElement newItem = xd.CreateElement("item");
XmlElement newID = xd.CreateElement("id");
XmlElement newName = xd.CreateElement("name");
XmlElement newNumber = xd.CreateElement("number");
//配参
newItem.SetAttribute("belong", item.Belong); //设置属性
newID.InnerText = item.Id; //设置内容
newName.InnerText = https://www.360docs.net/doc/f84162051.html,;
newNumber.InnerText = item.Number;
//装配,实现其组织结构
root.AppendChild(newItem);
newItem.AppendChild(newID);
newItem.AppendChild(newName);
newItem.AppendChild(newNumber);
//保存xml文档
xd.Save(Server.MapPath("Vote.xml"));
}
///
/// ylb:3, 修改一项
///
///
private void Update(VoteInfo vote)
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("Vote.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//获取节点类表
XmlNodeList items = root.ChildNodes;
//循环节点
foreach (XmlNode item in items)
{
//再循环节点
foreach (XmlNode p in item)
{
if (https://www.360docs.net/doc/f84162051.html, == "id" && p.InnerText == vote.Id)
{
//则修改这一项
//重设belong的值
item.Attributes["belong"].InnerText = vote.Belong;
//((XmlElement)item).SetAttribute("belong", vote.Belong);
//给该节点(id)下的节点赋值
p.NextSibling.InnerText = https://www.360docs.net/doc/f84162051.html,;
p.NextSibling.NextSibling.InnerText = vote.Number;
}
}
}
//保存xml文档
xd.Save(Server.MapPath("Vote.xml"));
}
///
/// ylb:4, 删除一项
///
///
private void Delete(string id)
{
//提取xml文档
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("vote.xml"));
//获取根节点
XmlNode root = xd.DocumentElement;
//获取节点列表
XmlNodeList items = root.ChildNodes;
//循环节点
foreach (XmlNode item in items)
{
foreach (XmlNode p in item)
{
if (https://www.360docs.net/doc/f84162051.html, == "id" && p.InnerText == id)
{
//移除该项
root.RemoveChild(item);
}
}
}
//保存xml文档
xd.Save(Server.MapPath("Vote.xml"));
}
protected void Page_Load(object sender, EventArgs e)
{
//调用展示
//ylb: 2,
VoteInfo item = new VoteInfo("袁博自传", "4", "ylb", "100");
//Add(item);
//ylb: 3, 根据id=3,修改belong="天涯" name="莫离",number=110 VoteInfo item2 = new VoteInfo("天涯", "3", "莫离", "110");
//Update(item2);
//ylb: 4, 删除id=4的项
Delete("3");
//ylb: 1, 遍历Xml文档
//BianLi();
}
}
复制代码
6,示例|讲解案例下载
博客园讲解:
https://www.360docs.net/doc/f84162051.html,/
百度文库开发文档:
https://www.360docs.net/doc/f84162051.html,/?business&aid=6&un=ylbtech#7
谷歌开源代码下载:
https://www.360docs.net/doc/f84162051.html,/p/ylbtechaspnet/downloads/list
请单击“ylbtechXml100010010”
warn 作者:ylbtech
出处:https://www.360docs.net/doc/f84162051.html,/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: https://www.360docs.net/doc/f84162051.html,, Xml
标签: https://www.360docs.net/doc/f84162051.html,, ylbtech, Xml, https://www.360docs.net/doc/f84162051.html, xml
绿色通道:好文要顶关注我收藏该文与我联系
ylbtech
关注- 43
粉丝- 108
+加关注
1 0
(请您对文章做出评价)
? 上一篇:SQL Server-存储过程(Procedure),带入参数和出参数
? 下一篇:https://www.360docs.net/doc/f84162051.html, Xml: DataSet的ReadXml(), WriteXml()和Response写Xml文档