ASP.net Xml: ASP.net操作Xml

 专题图ylbtech-asp.net-logo编号:ylbtechASPnetXml100010010

 

 

XML课件PPT【在线PPT课件倡导者-ylb】

 

   http://wenku.baidu.com/view/bfac3ebe1a37f111f1855bc2.html

 

1,功能描述

   这是一个基于.net操作Xml的案例示例,对Vote.xml文档的CRUD(增读改删)操作。本配有PPT课件供大家参考学习。

 

2,技术与环境

 

操作系统:

windows

开发语言:

C#

开发框架:

 

数据库:

开发软件:

Microsoft Visual Studio 2010

开发技术:

 ASP.net+Xml

课程总策划:

yuanbo

成员:

null

个人主页:

http://www.cnblogs.com/ylbtech/

科研团队:

ylbtech

教研团队:

ylbtech

 

3,/Vote.xml
<?xml version="1.0" encoding="utf-8"?>
<vote>
  <item belong="三国演义">
    <id>1</id>
    <name>晓梅</name>
    <number>60</number>
  </item>
  <item belong="西游记">
    <id>2</id>
    <name>小骆</name>
    <number>34</number>
  </item>
  <item belong="天涯">
    <id>3</id>
    <name>莫离</name>
    <number>110</number>
  </item>
</vote>

 

4,/App_Code/VoteInfo.cs
using System;

/// <summary>
///Vote 的摘要说明
/// </summary>
public class VoteInfo
{
    // 1, Attributes
    /// <summary>
    ///  图书名称
    /// </summary>
    string _belong;
    /// <summary>
    /// 编号
    /// </summary>
    string _id;
    /// <summary>
    /// 作者
    /// </summary>
    string _name;
    /// <summary>
    /// 书本数量
    /// </summary>
    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
{
   
    /// <summary>
    /// ylb:1, 遍历xml文档
    /// </summary>
    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("<pre>");
        foreach (XmlNode item in items)
        { 
            //输出属性
            Response.Write(item.Attributes["belong"].Name+"="+item.Attributes["belong"].InnerText+"\t");
            //输出子节点
            foreach (XmlNode p in item)
            {
                Response.Write(p.Name+"="+p.InnerText+"\t");
            }
            Response.Write("\n");
        }
        Response.Write("</pre>");


    }

    /// <summary>
    /// ylb:2, 添加
    /// </summary>
    /// <param name="item"></param>
    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 = item.Name;
        newNumber.InnerText = item.Number;

        //装配,实现其组织结构
        root.AppendChild(newItem);

        newItem.AppendChild(newID);
        newItem.AppendChild(newName);
        newItem.AppendChild(newNumber);

        //保存xml文档
        xd.Save(Server.MapPath("Vote.xml"));
        
    }

    /// <summary>
    /// ylb:3, 修改一项
    /// </summary>
    /// <param name="vote"></param>
    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 (p.Name == "id" && p.InnerText == vote.Id)
                {
                    //则修改这一项

                    //重设belong的值
                    item.Attributes["belong"].InnerText = vote.Belong;
                    //((XmlElement)item).SetAttribute("belong", vote.Belong);

                    //给该节点(id)下的节点赋值
                    p.NextSibling.InnerText = vote.Name;
                    p.NextSibling.NextSibling.InnerText = vote.Number;
                }
            }
        }
        //保存xml文档
        xd.Save(Server.MapPath("Vote.xml"));
    }

    /// <summary>
    /// ylb:4, 删除一项
    /// </summary>
    /// <param name="id"></param>
    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 (p.Name == "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,示例|讲解案例下载

博客园讲解:

       http://ylbtech.cnblogs.com/

百度文库开发文档:

       http://passport.baidu.com/?business&aid=6&un=ylbtech#7

谷歌开源代码下载:

       http://code.google.com/p/ylbtechaspnet/downloads/list

请单击“ylbtechXml100010010”

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted on 2012-08-16 07:30  ylbtech  阅读(8598)  评论(1编辑  收藏  举报