Code: C # Code
using System; using System.Data;
using System.Xml;
using System.IO;
namespace paladin
(
/ / / <summary>
/ / / ToXml a summary description.
/ / / Author: paladin.
/ / / Last Update Date: 2004-6-14 16:00
/ / / YuanDa Corporation, 2003-2010 All Rights Reserved
/ / /
/ / / One, use the MSXML (microsoft’s xml parser). MSXML is a com-based components.
/ / / DOMDocument40 doc = new DOMDocument40 ();
/ / / Doc.load ( “books.xml”);
/ / / 2, use the net framework comes with xml class. xmlreader, xmlwriter.
/ / / XmlTextReader, xmlTextWriter, xmlNodeReader, XmlValidatingReader.
/ / / 3, use the DOM (Document Object Model)
/ / / XmlDocument doc = new xnlDocument ();
/ / / XmlDocument with xmlreader, xmlwriter different, it has read and write capabilities, you can random access DOM tree. Similar to the MSXML’s DOM implementation.
/ / / 4, using Xpath and Xslt.
/ / / Xpath is the XML query language. XSlt is the basic structure of the document into another document or a different type.
/ / / 5, XML and ADO.NET
/ / / XML and ADO.NET into each other.
/ / / To access xml file can be used XmlTextWriter, XmlTextReader to visit. DOM can also be used to access the document template.
/ / / Read xml data can be used XmlTextReader, DOM or ADO.net
/ / / </ Summary>
public class ToXml
(
public ToXml ()
(
/ /
/ / TODO: Add constructor logic here
/ /
)
/ / / <summary>
/ / / Create an xml file, including the root element.
/ / / </ Summary>
/ / / <param Name=”file”> </ param>
/ / / <param Name=”rootElement”> </ param>
public void CreateXml (string file, string rootElement)
(
XmlDocument doc = new XmlDocument ();
/ / Create a new XmlDeclaration, version 1.0, code (default utf8) and standalone flag
XmlDeclaration newDec = doc.CreateXmlDeclaration ( “1.0″, null, null);
doc.AppendChild (newDec);
/ / Create a root element
XmlElement newRoot = doc.CreateElement (rootElement);
doc.AppendChild (newRoot);
/ / xml file is written to disk
doc.Save (file);
)
/ / / <summary>
/ / / Create an xml file, xml contents of the file has been constructed in the xmlStr good.
/ / / </ Summary>
/ / / <param Name=”xmlStr”> </ param>
public void CreateXml (string xmlStr)
(
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xmlStr);
doc.Save (System.Web.HttpContext.Current.Server.MapPath ( “/ newengnieer / includes / categoryTree.xml”));
)
/ / / <summary>
/ / / Add an xml element, including the element sub-elements and attributes.
/ / / </ Summary>
/ / / <param Name=”file”> </ param>
public void InsertXml (string file)
(
XmlDocument doc = new XmlDocument ();
doc.Load (file);
/ / Create a new element
XmlElement newBook = doc.CreateElement ( “book”);
/ / Create the element properties
newBook.SetAttribute ( “aa”, “ss”);
newBook.SetAttribute ( “ss”, “dd”);
/ / Create the element sub-elements
XmlElement newtitle = doc.CreateElement ( “title”);
newtitle.InnerText = “sdfdf”;
newBook.AppendChild (newtitle);
/ / Create the element sub-elements
XmlElement newauthor = doc.CreateElement ( “author”);
newBook.AppendChild (newauthor);
/ /
doc.DocumentElement.AppendChild (newBook);
/ / xml file is written to disk
XmlTextWriter tr = new XmlTextWriter (file, null);
tr.Formatting = Formatting.Indented;
doc.WriteContentTo (tr);
tr.Close ();
)
/ / / <summary>
/ / / Use the XmlTextWriter to write xml
/ / / </ Summary>
private void writeToXml (string fileName)
(
/ / xml file path
/ / String fileName = MapPath ( “ucTabBarConfig.xml”);
XmlTextWriter xmltw = new XmlTextWriter (fileName, System.Text.Encoding.Unicode);
xmltw.Formatting = Formatting.Indented;
/ / Start writing xml document
xmltw.WriteStartDocument ();
/ / Start writing the document element
xmltw.WriteStartElement ( “tabBar1″);
/ / Start writing elements ‘tabBar1′ element in the document under ‘tab1′
xmltw.WriteStartElement ( “tab1″);
xmltw.WriteElementString ( “text”, “violation placed on file”);
xmltw.WriteElementString ( “href “,”");
xmltw.WriteEndElement ();
/ / Elements of completed write
xmltw.WriteStartElement ( “tab2″);
xmltw.WriteElementString ( “text”, “Administrative Punishment Opinion”);
xmltw.WriteElementString ( “href “,”");
xmltw.WriteEndElement ();
/ /
xmltw.WriteStartElement ( “tab3″);
xmltw.WriteElementString ( “text”, “extra”);
xmltw.WriteElementString ( “href “,”");
xmltw.WriteEndElement ();
/ /
xmltw.WriteStartElement ( “tab4″);
xmltw.WriteElementString ( “text”, “administrative penalty charges”);
xmltw.WriteElementString ( “href “,”");
xmltw.WriteEndElement ();
/ / Elements of completed write
xmltw.WriteEndElement ();
/ / Write the document has finished
xmltw.WriteEndDocument ();
/ /
xmltw.Flush ();
xmltw.Close ();
)
/ / / <summary>
/ / / Read xml data using xmlTextReader
/ / / </ Summary>
/ / / <param Name=”fileName”> </ param>
/ / / <returns> </ Returns>
private string readFromXml (string fileName)
(
/ / xml file path
/ / String fileName = MapPath ( “ucTabBarConfig.xml”);
XmlTextReader xmltr = new XmlTextReader (fileName);
/ /
string returnValue = “”;
while (xmltr.Read ())
(
if (xmltr.NodeType == XmlNodeType.Text)
(
returnValue = returnValue + xmltr.Value;
)
)
return returnValue;
)
/ / / <summary>
/ / / Read xml data using Ado.net
/ / / </ Summary>
/ / / <returns> </ Returns>
private DataView readDataFromXml (string fileName)
(
DataSet ds = new DataSet ();
ds.ReadXml (fileName);
return ds.Tables [0]. DefaultView;
)
/ / / <summary>
/ / / Under strSearch conditions to find the
/ / / </ Summary>
/ / / <param Name=”file”> </ param>
/ / / <param Name=”strSearch”> </ param>
/ / / <returns> </ Returns>
public string searchXml (string file, string strSearch)
(
string returnVal = “”;
XmlDocument doc = new XmlDocument ();
doc.Load (file);
/ / Find <root>
/ / <book Name=”文明”>
/ / <title> Dfdf </ title>
/ / </ Book>
/ / </ Root>
/ / Find a child element of book elements innerText “sdfdf” item
/ / string strSearch = “root / book [title = 'sdfdf']“;
/ / Find element attribute name for the book “civilization” entry
/ / string strSearch = “root / book [@ name = 'civilized']“;
XmlNode serchNode = doc.SelectSingleNode (strSearch);
if (serchNode! = null)
(
returnVal = serchNode.InnerText;
)
return returnVal;
)
/ / / <summary>
/ / / Based on TagName to find the
/ / / </ Summary>
/ / / <param Name=”file”> </ param>
/ / / <param Name=”TagName”> </ param>
public void searchXmlByTag (string file, string TagName)
(
XmlDocument doc = new XmlDocument ();
doc.Load (file);
/ / Find <title> </ title>
XmlNodeList nodeList = doc.GetElementsByTagName ( “title”);
foreach (XmlNode node in nodeList)
(
/ / ListBox1.Items.Add (node.InnerText);
)
)
)
)