.NET操作XML文件---[添加]

本文档通过实例展示了.NET框架如何读取、修改和保存XML文件,详细解释了相关代码实现。
摘要由CSDN通过智能技术生成
最近学习了.NET操作XML文件,总结如下:
关于XML
全名:可扩展标记语言 (Extensible Markup Language)
XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。

XML与数据库区别:
数据库提供了更强有力的数据存储和分析能力,例如:数据索引、排序、查找、相关一致性等;
XML仅仅是存储数据;

下面通过实例来介绍.NET如何操作XML

XMLFILE.xml文件的具体信息如下:

<?xml version="1.0" encoding="utf-8"?>
<Pizza>
  <Pizzas id="1" size="6" price="18">香菇火腿披萨</Pizzas>
  <Pizzas id="2" size="6" price="18">什蔬香肠披萨</Pizzas>
  <Pizzas id="3" size="6" price="18">熏鸡肉蘑菇披萨</Pizzas>
  <Pizzas id="4" size="6" price="18">玛格丽特披萨</Pizzas>
  <Pizzas id="5" size="6" price="28">摩洛哥披萨</Pizzas>
</Pizza>

showXml.aspx文件与 XMLFILE.xml文件的路径关系:


showXml.aspx文件的详情:

效果图:


代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showXml.aspx.cs" Inherits="showXml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>披萨名称:</td>
    <td><asp:TextBox ID="tbPizzaName" runat="server" Text=""></asp:TextBox></td>
    </tr>
    <tr>
    <td>披萨大小:</td>
    <td><asp:TextBox ID="tbPizzaSize" runat="server" Text=""></asp:TextBox></td>
    </tr>
    <tr>
    <td>披萨价格:</td>
    <td><asp:TextBox ID="tbPizzaPrice" runat="server" Text=""></asp:TextBox></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><asp:Label ID="lblMes" runat="server" Text=""></asp:Label></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><asp:Button ID="btnAdd" runat="server" Text="添加新披萨" οnclick="btnAdd_Click" /></td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>

showXml.aspx.cs的代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml;//用于XMl操作

public partial class showXml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (tbPizzaName.Text.Trim() == "" || tbPizzaPrice.Text.Trim() == "" || tbPizzaSize.Text.Trim() == "")
        {
            lblMes.Text = "请完善披萨的信息!";
        }
        else
        {
            bool flag=AddPizza();
            if (flag)
            {
                lblMes.Text = "新披萨的信息添加成功!";
            }
        }
    }

    protected bool AddPizza()
    {
        //初始化id
        int newId;
        //创建XML文件对象的实例doc
        XmlDocument doc = new XmlDocument();
        //加载XML文件
        doc.Load(HttpContext.Current.Server.MapPath("XMLFile.xml"));
        //获取结点Pizza下的所有子结点
        XmlNodeList nodeList = doc.SelectSingleNode("Pizza").ChildNodes;
        if (nodeList.Count > 0)
        {
            //查找最后一个结点的id
            newId = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("/Pizza/Pizzas[last()]").Attributes["id"].Value) + 1;
        }
        else
        {
            newId = 1;
        }
        //创建一个新的xml元素
        XmlElement Pizzas = doc.CreateElement("Pizzas");
        //创建xml属性
        XmlAttribute id = doc.CreateAttribute("id");
        XmlAttribute size = doc.CreateAttribute("size");
        XmlAttribute price = doc.CreateAttribute("price");
        //给xml属性赋值
        id.Value = newId.ToString();
        size.Value = tbPizzaSize.Text;
        price.Value = tbPizzaPrice.Text;
        //给结点赋值
        Pizzas.InnerText = tbPizzaName.Text;
        //把属性值添加到元素结点里
        Pizzas.Attributes.Append(id);
        Pizzas.Attributes.Append(size);
        Pizzas.Attributes.Append(price);
        //把元素结点添加到XMl文件里
        doc.DocumentElement.AppendChild(Pizzas);
        //保存XML文件
        doc.Save(HttpContext.Current.Server.MapPath("XMLFile.xml"));
        return true;
    }
}
<未完待续>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值