C#操作XML的方法是什么
发表于:2024-10-02 作者:千家信息网编辑
千家信息网最后更新 2024年10月02日,本篇内容主要讲解"C#操作XML的方法是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#操作XML的方法是什么"吧!C# 操作XML的全过程实例第一
千家信息网最后更新 2024年10月02日C#操作XML的方法是什么
本篇内容主要讲解"C#操作XML的方法是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#操作XML的方法是什么"吧!
C# 操作XML的全过程实例第一步:
在项目文件里创建db文件夹,在db文件夹里创建db.xml文档。db.xml结构只包含:﹤Group﹥﹤/Group﹥
C# 操作XML的全过程实例第二步:
新建添加留言和显示留言的两个画面:MessageAdd.aspx和MessageList.aspx。下面是两个页面的 前台和后台源码。
C# 操作XML的全过程实例代码:
//-----Begin------// 1.MessageAdd.aspx //C# 操作XML的全过程实例 ﹤%@ Page language="c#" Codebehind="MessageAdd.aspx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageAdd" %﹥ ﹤!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ﹥ ﹤HTML﹥ ﹤HEAD﹥ ﹤title﹥MessageAdd﹤/title﹥ ﹤meta content="Microsoft Visual Studio 7.0" name="GENERATOR"﹥ ﹤meta content="C#" name="CODE_LANGUAGE"﹥ ﹤meta content="JavaScript" name="vs_defaultClientScript"﹥ ﹤meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"﹥ ﹤style﹥.Txt1 { FONT-SIZE: 12px } ﹤/style﹥ ﹤/HEAD﹥ ﹤body﹥ ﹤form id="MessageAdd" method="post" runat="server"﹥ ﹤FONT face="宋体"﹥ ﹤P﹥ ﹤/P﹥ ﹤P align="center"﹥ ﹤TABLE class="Txt1" id="Table1" cellSpacing="1" cellPadding="4" width="749" bgColor="#efefef" border="0"﹥ ﹤TR﹥ ﹤TD bgColor="#e8dabd"﹥您的大名﹤/TD﹥ ﹤TD style="WIDTH: 508px" bgColor="#ffffff"﹥ ﹤asp:textbox id="TextBoxUName" runat="server" LeaveSQL="False" LeaveFormat="False" HtmlEncode="False" Width="519px" MaxLength="10" CssClass= "TextAreaClass"﹥﹤/asp:textbox﹥﹤/TD﹥ ﹤TD bgColor="#ffffff" colSpan="1" rowSpan="1"﹥﹤/TD﹥ ﹤/TR﹥ ﹤TR﹥ ﹤TD bgColor="#e8dabd" colSpan="1" rowSpan="1"﹥E-Mail﹤/TD﹥ ﹤TD style="WIDTH: 505px" bgColor="#ffffff"﹥ ﹤P﹥﹤asp:textbox id="TextBoxMail" runat="server" LeaveSQL="False" LeaveFormat="False" HtmlEncode="False" Width="519px" MaxLength="30" CssClass="TextAreaClass"﹥ ﹤/asp:textbox﹥﹤/P﹥ ﹤/TD﹥ ﹤TD bgColor="#ffffff" colSpan="1" rowSpan="1"﹥ ﹤BR﹥ //C# 操作XML的全过程实例 ﹤/TD﹥ ﹤/TR﹥ ﹤TR﹥ ﹤TD bgColor="#e8dabd" colSpan="1" rowSpan="1"﹥内容﹤/TD﹥ ﹤TD style="WIDTH: 505px" bgColor="#ffffff"﹥ ﹤P﹥﹤asp:textbox id="TextBoxTxt" runat="server" LeaveSQL="False" LeaveFormat="True" HtmlEncode="False" Width="519px" TextMode="MultiLine" Height="160px"﹥﹤/asp:textbox﹥﹤/P﹥ ﹤/TD﹥ ﹤TD bgColor="#ffffff" colSpan="1" rowSpan="1"﹥﹤/TD﹥ ﹤/TR﹥ ﹤/TABLE﹥ ﹤/P﹥ ﹤P align="center"﹥ ﹤asp:button id="Button1" runat="server" CssClass="TextAreaClass" Text=" 提交 "﹥ ﹤/asp:button﹥ ﹤INPUT class="TextAreaClass" onclick=_window.location.replace('MessageList.aspx')" type="button" value=" 返回 "﹥﹤/P﹥ ﹤/FONT﹥ ﹤/form﹥ ﹤/body﹥ ﹤/HTML﹥ MessageAdd.cs //C# 操作XML的全过程实例using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Xml; namespace XMLGuest { /// ﹤summary﹥ /// MessageAdd 的摘要说明。 /// ﹤/summary﹥ public class MessageAdd : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox TextBoxUName; protected System.Web.UI.WebControls.TextBox TextBoxMail; protected System.Web.UI.WebControls.TextBox TextBoxTxt; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { //C# 操作XML的全过程实例 } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// ﹤summary﹥ /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// ﹤/summary﹥ private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { //C# 操作XML的全过程实例 XmlDocument doc = new XmlDocument(); doc.Load(MapPath("db\\db.xml")); XmlNode node = doc.CreateElement("Item"); XmlAttribute xmlattr = doc.CreateAttribute("UserName"); xmlattr.Value = XmlConvert.DecodeName(TextBoxUName.Text); node.Attributes.Append(xmlattr); xmlattr = doc.CreateAttribute("UserEmail"); xmlattr.Value = XmlConvert.DecodeName(TextBoxMail.Text); node.Attributes.Append(xmlattr); xmlattr = doc.CreateAttribute("Content"); xmlattr.Value = XmlConvert.DecodeName(TextBoxTxt.Text); node.Attributes.Append(xmlattr); xmlattr = doc.CreateAttribute("SendTime"); xmlattr.Value = XmlConvert.DecodeName(DateTime.Now.ToString()); node.Attributes.Append(xmlattr); doc.ChildNodes[0].AppendChild(node); doc.Save(MapPath("db\\db.xml")); Response.Redirect("MessageList.aspx"); } } //C# 操作XML的全过程实例} 2.MessageList.aspx ﹤%@ Page language="c#" Codebehind="MessageList.aspx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageList" %﹥ ﹤!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ﹥ ﹤HTML﹥ ﹤HEAD﹥ ﹤title﹥MessageList﹤/title﹥ ﹤meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"﹥ ﹤meta name="CODE_LANGUAGE" Content="C#"﹥ ﹤meta name="vs_defaultClientScript" content="JavaScript"﹥ ﹤meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"﹥ ﹤style﹥ .body { FONT-SIZE: 12px } .td1 { FONT-SIZE: 12px } ﹤/style﹥ ﹤/HEAD﹥ //C# 操作XML的全过程实例 ﹤body class="body"﹥ ﹤form id="MessageList" method="post" runat="server"﹥ ﹤P align="right"﹥ ﹤asp:HyperLink id="GrowHyperLink1" runat="server" Text="[留言]" NavigateUrl="MessageAdd.aspx"﹥﹤/asp:HyperLink﹥ ﹤/P﹥ ﹤P align="center"﹥﹤FONT face="宋体"﹥﹤/FONT﹥ ﹤FONT face="宋体"﹥﹤/FONT﹥﹤BR﹥ ﹤asp:DataGrid id="DataList1" runat="server" AutoGenerateColumns="False" ShowHeader="False" PageSize="15" BorderStyle="None" BorderWidth="0px" CellPadding="4" Font-Size="12px"﹥ ﹤Columns﹥ ﹤asp:TemplateColumn﹥ ﹤ItemTemplate﹥ ﹤TABLE id="Table7" borderColor="#efefef" cellSpacing="0" cellPadding="4" width="749" bgColor="#efefef" border="1"﹥ ﹤TR class="td1" bgColor="#fafafa"﹥ ﹤TD style="WIDTH: 200px" bgColor="#ffffff"﹥ ﹤STRONG﹥姓名:﹤/STRONG﹥ ﹤asp:Label id=Label5 runat="server" Text=' ﹤%# DataBinder.Eval(Container, "DataItem.UserName").ToString() %﹥'﹥ ﹤/asp:Label﹥ ﹤/TD﹥ ﹤TD style="WIDTH: 150px" bgColor="#ffffff"﹥ ﹤STRONG﹥﹤FONT face="宋体"﹥时间﹤/FONT﹥ ﹤/STRONG﹥ ﹤STRONG﹥﹤FONT face="宋体"﹥:﹤/FONT﹥ ﹤/STRONG﹥ ﹤asp:Label id=Label4 runat="server" Text=' ﹤%# DataBinder.Eval(Container, "DataItem.SendTime").ToString() %﹥'﹥ ﹤/asp:Label﹥﹤/TD﹥ ﹤TD bgColor="#ffffff"﹥﹤STRONG﹥E-Mail﹤/STRONG﹥ ﹤STRONG﹥: ﹤asp:HyperLink id=HyperLink1 runat="server" NavigateUrl=' ﹤%# "mailto:"+DataBinder.Eval(Container, "DataItem.UserEmail"). ToString() %﹥' Text='﹤%# DataBinder.Eval(Container, "DataItem.UserEmail").ToString() %﹥'﹥ ﹤/asp:HyperLink﹥﹤/STRONG﹥﹤/TD﹥ ﹤/TR﹥ ﹤TR class="td1"﹥ ﹤TD bgColor="#ffffff" colSpan="3"﹥ ﹤asp:Label id=Label1 runat="server" Text=' ﹤%# DataBinder.Eval(Container, "DataItem.Content").ToString() %﹥'﹥ ﹤/asp:Label﹥﹤/TD﹥ ﹤/TR﹥ ﹤/TABLE﹥ ﹤/ItemTemplate﹥ ﹤/asp:TemplateColumn﹥ ﹤/Columns﹥ ﹤PagerStyle Visible="False"﹥ ﹤/PagerStyle﹥ ﹤/asp:DataGrid﹥ ﹤/P﹥ ﹤/form﹥ ﹤/body﹥ ﹤/HTML﹥ //C# 操作XML的全过程实例MessageList.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; namespace XMLGuest { //C# 操作XML的全过程实例 /// ﹤summary﹥ /// MessageList 的摘要说明。 /// ﹤/summary﹥ public class MessageList : System.Web.UI.Page { protected HyperLink GrowHyperLink1; protected System.Web.UI.WebControls.DataGrid DataList1; private void Page_Load(object sender, System.EventArgs e) { FileStream stream = new FileStream( Server.MapPath("db\\db.xml"),FileMode.Open); try { XmlDataDocument doc = new XmlDataDocument(); doc.DataSet.ReadXml(new StreamReader(stream)); if(doc.DataSet.Tables.Count != 0 && doc.DataSet.Tables[0].Rows.Count!=0) { DataList1.DataSource = doc.DataSet.Tables[0].DefaultView ; DataList1.DataBind(); } stream.Close(); } catch(Exception ding) { Response.Write(ding.Message); } finally { stream.Close(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } //C# 操作XML的全过程实例 /// ﹤summary﹥ /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// ﹤/summary﹥ private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } } //---End----//
-
到此,相信大家对"C#操作XML的方法是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
全过程
实例
C#
方法
内容
宋体
设计
代码
文件
两个
摘要
文件夹
此方法
窗体
编辑器
c#
学习
支持
实用
更深
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
app直销系统软件开发
选择数据库创建表的语法
网络安全考研考线性代数
网吧服务器固态硬盘
方舟手游龙珠服务器怎么进去
读我网络技术
神通收银客户端数据库
软件开发与测试表情包
杭州工行软件开发中心加班多吗
兴安区网络安全
计算机基础知识与网络安全
苹果笔记本用什么软件开发
硬盘数据自动上传服务器
改变数据库表中数据
网络安全法的颁布积极影响
数据库安全性控制 实验报告
武隆区工商软件开发流程价目表
创建数据库及其表结构
中国互联网大会法律科技论坛
陶云网络技术有限公司
海康流媒体服务器软件官网下载
r510服务器安装黑群晖
上海企业网络技术服务哪家好
互联网科技公司门户官网
管理erp软件开发
无线网络的软件开发难吗
想学好网络技术怎么办
一般一个软件开发团队
排序英文数据库
一个系统只有三张数据库表