Due to the easy sharing of XML and many other advantages, XML technology are increasingly being used in corporate data processing and other fields, such as statements of the enterprise application, press releases, accounting data processing and so on.
XML is fast becoming a middle layer from the desktop to the data transfer tools, as the XML data can be in the middle layer and multi-agent back-end (database) source integration, the vast majority of the current database maker has the full support of XML technology, provided the Kind of processing power of XML data.
Microsoft's. NET around the core XML provides a strong and rapid development tools - C #, it is an unprecedented development of high-efficiency, especially in the aspects of XML programming.
C # provides a number of related processing XML data, such as the type of stream processing: XmlReader and XmlWriter; DOM class: XmlNode, XmlDocument and XmlElement, and so on; Xpath class: XmlNavigator; XSLT class: XslTransform.
Display the contents of XML documents
Using C # Programming XML documents show that the information is used. NET provides a standard type of XML file to read the contents of a StreamReader class of objects, the DataSet class XmlDataDocument then read the XML approach to read XML message to the DataSet, DataSet and then DataView way to assign a Web Form on the DataGrid, by the end DataBind data show that specific code is as follows:
Using System.Xml;
/ / XML have to deal with the increase Namespace, also References in Canada System.XML.Dll
Using System.IO;
/ / Read XML documents must increase the Namespace
Page_Load and then add the following code:
protected void Page_Load (object sender, EventArgs e) (
String datafile = "guest.xml";
/ / Assume that XML file called guest.xml
StreamReader tyj = new StreamReader (Server.MapPath (datafile));
XmlDataDocument datadoc = new XmlDataDocument ();
/ / Create the XML object in order to read
Datadoc.DataSet.ReadXml (tyj);
/ / Read the contents of the documents guest.xml
DataGrid1.DataSource = datadoc.DataSet.Tables [0]. DefaultView;
/ / Set the data source DataGrid
DataGrid1.DataBind ();
/ / Bind
Datadoc = null;
/ / Release resources
Tyj.Close ();)
/ / Release StreamReader category, which is very important, otherwise the next show will open the documents have been used
Corresponds to the show by the Web Form in the DataGrid, we need to increase the following functions:
protected void OnSelectName (object sender, EventArgs e) (
Session [ "select_name"] = (string) DataGrid1.SelectedItem.Cells [1]. Text.ToString ();
/ / DataGrid to a selected line of a unit of value (Name) into a session variable, with a view to the next page
Response.Redirect ( "xml_manage.aspx");)
/ / Delete function to increase the management page
Web Form by adding the following code:
<asp:DataGrid Id=DataGrid1 runat="server" onselectedindexchanged="OnSelectName">
<property Name="Columns">
<asp:buttoncolumn Text="选择" commandname="Select" />
</ Property>
Code underlined the role of the press when the "choice" button, the implementation of the OnSelectName () in the process, the DataGrid selected in a line of a unit of value (Name) into a session variable, And then go to the next page.
Increase the content of XML documents
Web Form in a corresponding increase in four Label (name, from where, Email address, message content) and four for a TextBox and submitted Button, and this button to add the following code:
string datafile = "guest.xml"; XmlDocument xmldocument = new XmlDocument ();
xmldocument.Load (Server.MapPath (datafile));
/ / Guest.xml to read in xmldocument
DocumentNavigator navigator = new DocumentNavigator (xmldocument);
/ / The most important category
Navigator.MoveToDocumentElement ();
navigator.Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Element, "Guest ","",""); / / insert nodes Guest
navigator.Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Element, "Name ","","");
navigator.Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Text, "Name ","","");
Navigator.Value = Name.Text;
/ / Node for assignment
Navigator.MoveToParent ();
/ / Return to the parent node Guest
... ...
/ / Use the same statement, in the Name element inserted under the other elements, such as Country, E-mail address and message, and so on
Xmldocument.Save (Server.MapPath (datafile));
/ / Finally, save the XML document
Navigator = null;
Xmldocument = null;
/ / XML documents released so that other programs can use it
DocumentNavigator category code using the above-mentioned elements and increase the contents of the attention after the release of the use of resources.
To delete the contents of XML documents
To delete the selected record, for you to choose the top node, the following code to find the node and select clear message:
String datafile = "guest.xml";
XmlDocument xmldocument = new XmlDocument ();
xmldocument.Load (Server.MapPath (datafile));
/ / Guest.xml to read in xmldocument
DocumentNavigator navigator = new DocumentNavigator (xmldocument);
Navigator.MoveToDocumentElement ();
navigator.Select ( "/ Guests / Guest [Name =" "+ Session [" select_name "]+""]");
/ / Parameter is the XPath
Navigator.RemoveSelected ();
/ / Delete the implementation of the
Xmldocument.Save (Server.MapPath (datafile));
/ / Finally, save the XML document
Navigator = null;
/ / Release category
Xmldocument = null;
/ / XML documents released so that other programs can use it
For the XML file to remove all of the information, the use of "navigator.RemoveChildren ();" statement can be realized.
Conclusion
Overview on the known, C # preparation of XML applications not only quick and convenient, in the preparation of ASP.NET database applications, using XML files to replace some of the small Table, to reduce the number of connections to access the database, other networks can also use these procedures more Data.
XML is currently the main bottleneck is the file system read or write, it should be used more cache memory and the way, if not a tremendous amount of information and also revised smaller, and great views, using the XML approach would be very A good choice; On the contrary, such a huge amount of data, consideration should be given to support the use of XML database, whether you use ADO + connect to other databases, or use SQL server, C # have a direct function allows these types of database information and direct XML interactive visit. |