熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用XmlDocument創建XML文檔

2022-06-13   來源: .NET編程 

  代碼如下  using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemXml;
namespace XMLDOMDemo
{

  public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
private void btnLoad_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
MessageBoxShow(xmlDocInnerXml);
}
//創建文檔
private void btnCreate_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定義聲明
XmlDeclaration dec = xmlDocCreateXmlDeclaration( GB null);
xmlDocAppendChild(dec);
//創建根節點
XmlElement root = xmlDocCreateElement(Books);
xmlDocAppendChild(root);
XmlNode book = xmlDocCreateElement(Book);
XmlElement title = xmlDocCreateElement(Title);
titleInnerText = SQL Server;
bookAppendChild(title);
XmlElement isbn = xmlDocCreateElement(ISBN);
isbnInnerText = ;
bookAppendChild(isbn);
XmlElement author = xmlDocCreateElement(Author);
authorInnerText = jia;
bookAppendChild(author);
XmlElement price = xmlDocCreateElement(Price);
priceInnerText = ;
priceSetAttribute(Unit ___FCKpd___quot;);
bookAppendChild(price);
rootAppendChild(book);
xmlDocSave(Booksxml);
}

  private void btnInsert_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
XmlNode root = xmlDocSelectSingleNode(Books);
XmlElement book = xmlDocCreateElement(Book);
XmlElement title = xmlDocCreateElement(Title);
titleInnerText = XML;
bookAppendChild(title);
XmlElement isbn = xmlDocCreateElement(ISBN);
isbnInnerText = ;
bookAppendChild(isbn);
XmlElement author = xmlDocCreateElement(Author);
authorInnerText = snow;
bookAppendChild(author);
XmlElement price = xmlDocCreateElement(Price);
priceInnerText = ;
priceSetAttribute(Unit ___FCKpd___quot;);
bookAppendChild(price);
rootAppendChild(book);
xmlDocSave(Booksxml);
MessageBoxShow(數據已寫入!);
}

  private void btnUpdate_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
////Book[@Unit=\$\]
//獲取Books節點的所有子節點
XmlNodeList nodeList = xmlDocSelectSingleNode(Books//Book)ChildNodes;
//遍歷所有子節點
foreach (XmlNode xn in nodeList)
{
//將子節點類型轉換為XmlElement類型
XmlElement xe = (XmlElement)xn;
if (xeName == Author)
{
xeInnerText = amandag;
}
if (xeGetAttribute(Unit) == ___FCKpd___quot;)
{
xeSetAttribute(Unit );
}
}
xmlDocSave(Booksxml);
}

  private void btnDelete_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
XmlNodeList nodeList = xmlDocSelectSingleNode(Books//Book)ChildNodes;
//遍歷所有子節點
foreach (XmlNode xn in nodeList)
{
//將子節點類型轉換為XmlElement類型
XmlElement xe = (XmlElement)xn;
if (xeName == Author)
{
xeRemoveAll();
}
if (xeGetAttribute(Unit) == )
{
xeRemoveAttribute(Unit);
}
}
xmlDocSave(Booksxml);
}
}
}


From:http://tw.wingwit.com/Article/program/net/201311/11947.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.