日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.NET平台开发必须掌握的XML知识(二)

發(fā)布時間:2023/12/31 asp.net 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET平台开发必须掌握的XML知识(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*********??? 程序設(shè)計目的:往已經(jīng)存在的XML文件中添加新的節(jié)點,并把節(jié)點讀出來顯示在列表框中,同時練習(xí)怎么刪除節(jié)點。?? ********/
/*********??? 一共用了一個窗體,四個按紐,一個列表框????????????????????????????????????????????????????????????????? ********/
/*********??? 主要運用的System.Xml命名空間的類的屬性和方法??????????????????????????????????????????????????????????? ********/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace 操作XML文件
{
??? public partial class Form1 : Form
??? {
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????? } private void btnLoopXml_Click(object sender, EventArgs e)
??????? {
??????????? //Clear ListBox
??????????? listBox1XmlNodes.Items.Clear(); //Load the XML document
??????????? XmlDocument document = new XmlDocument();
??????????? document.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml"); //Loop XML document
??????????? recurseXmlDocument((XmlNode)document.DocumentElement, 0); } //讀出節(jié)點并顯示出來
??????? private void recurseXmlDocument(XmlNode root, int indent)
??????? {
??????????? //Make sure we don't do anything if the root is null
??????????? if (root == null)
??????????? {
??????????????? return;
??????????? }
??????????? if (root is XmlElement) //Root is an XmlElement type
??????????? {
??????????????? //first print the name?
??????????????? listBox1XmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent)); //Then check if there are any child nodes and if there are,call this
??????????????? //method anain to print them
??????????????? if (root.HasChildNodes)
??????????????? {
??????????????????? recurseXmlDocument(root.FirstChild, indent + 2);
??????????????? } //Finally check tosee if there are any siblings and if there are?
??????????????? //call this method again to have them printed
??????????????? if (root.NextSibling != null)
??????????????????? recurseXmlDocument(root.NextSibling, indent);
??????????? }
??????????? else if(root is XmlText)
??????????? {
??????????????? //Print the text
??????????????? string text? = ((XmlText)root).Value;
??????????????? listBox1XmlNodes.Items.Add(text.PadLeft(text.Length+indent));
??????????? }
??????????? else if(root is XmlComment)
??????????? {
??????????????? //print text
??????????????? string text = root.Value;
??????????????? listBox1XmlNodes.Items.Add(text.PadLeft(text.Length+indent)); //Then check if there are any child nodes,and jf there are
??????????????? //method again to print them
??????????????? if(root.HasChildNodes)
??????????????? {
??????????????????? recurseXmlDocument(root.FirstChild,indent+2);
??????????????? }?
???????????????????
????????????????
???????????????? //Finally,check to see if there are any siblings,and if there are?
???????????????? //call this method again to have them printed.
??????????????? if (root.NextSibling!=null)
??????????????? {
??????????????????? recurseXmlDocument(root.NextSibling,indent);
??????????????? }
??????????? }
??????? } private void button1_Click(object sender, EventArgs e)
??????? {
???????????? MessageBox.Show("關(guān)閉窗口!");
???????????? this.Close();
??????? } //添加節(jié)點
??????? private void btnCreateNode_Click(object sender, EventArgs e)
??????? {
??????????? //Load the XML document
??????????? XmlDocument document = new XmlDocument();
??????????? document.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml"); //Get the root element
??????????? XmlElement root = document.DocumentElement; //create the new nodes
??????????? XmlElement newStory = document.CreateElement("story");
??????????? XmlElement newTitle = document.CreateElement("title");
??????????? XmlElement newAuthor = document.CreateElement("author");
??????????? XmlElement newName = document.CreateElement("name");
??????????? XmlElement newNationality = document.CreateElement("nationality");
??????????? XmlElement newRating = document.CreateElement("rating"); XmlComment comment = document.CreateComment("This story is the story you are reading!");
??????????? XmlText title = document.CreateTextNode("美麗人生");
??????????? XmlText name = document.CreateTextNode("王亞楠");
??????????? XmlText nationality = document.CreateTextNode("中國");
??????????? XmlText rating = document.CreateTextNode("智慧,美麗,時尚,活力");
?????????? //Insert the element
??????????? newStory.AppendChild(comment);
??????????? newStory.AppendChild(newTitle);
??????????? newStory.AppendChild(newAuthor);
??????????? newStory.AppendChild(newRating); newTitle.AppendChild(title);
?????????? newAuthor.AppendChild(newName);
??????????? newAuthor.AppendChild(newNationality);
?????????? newName.AppendChild(name);
??????????? newNationality.AppendChild(nationality); newRating.AppendChild(rating);
??????????? root.InsertAfter(newStory,root.LastChild);
??????????? root.InsertAfter(newStory,root.FirstChild);
??????????? document.Save(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");
??????? } //刪除節(jié)點
??????? private void btnDeleteNode_Click_1(object sender, EventArgs e)
??????? {
??????????? //Load XML document
??????????? XmlDocument document = new XmlDocument();
??????????? document.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml"); //Get the root element
??????????? XmlElement root = document.DocumentElement;
??????????? // Find the node.root is the <story>tag,soits last child
??????????? //while will be the last <book> node.
??????????? if (root.HasChildNodes)
??????????? {
??????????????? XmlNode story = root.LastChild; //Delete the child
??????????????? root.RemoveChild(story); //Save the document story to disk
??????????????? document.Save(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");
??????????? }
??????? }
?
??? }
}
//本程序是先利用btnCreateNode按紐往XML文件中寫入數(shù)據(jù),然后再利用btnLoopXml按紐讀出數(shù)據(jù)顯示在列表框listBox1XmlNodes中
//讀數(shù)據(jù)時主要利用了recurseXmlDocument(XmlNode root, int indent)方法迭代XML中的元素
//問題的關(guān)鍵在于recurseXmlDocument(XmlNode root, int indent)方法的編寫









本文轉(zhuǎn)自terryli51CTO博客,原文鏈接: http://blog.51cto.com/terryli/519420,如需轉(zhuǎn)載請自行聯(lián)系原作者


總結(jié)

以上是生活随笔為你收集整理的.NET平台开发必须掌握的XML知识(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。