XML file Operations

Source: Internet
Author: User
Tags add object string tostring xml reader xpath xsl
Xml

A Writing to an XML file


1/**////<summary>
2///write to XML file
3///</summary>
4private void WriteXml ()
5{
6/**////creates a string representing the path of the XML file to be generated. If the path points to an NTFS partition, the associated access rights are required.
7 string filename = Xmlfilepathtextbox.text;
8
9/**////Create a file stream to write XML data
System.IO.FileStream myFileStream = new System.IO.FileStream (filename, System.IO.FileMode.Create);
11
12/**////Create a XmlTextWriter object using a file Flow object
XmlTextWriter myxmlwriter = new XmlTextWriter (myFileStream, System.Text.Encoding.Unicode);
14
myxmlwriter.formatting = formatting.indented;
16
Try
18 {
19/**////writes data to the XmlTextWriter object using the Writexmlbyxmlwriter method
Writexmlbyxmlwriter (Myxmlwriter, "MSFT", 74.5, 5.5, 49020000);
21st
22/**////through the Close method call, the XmlTextWriter object's data is eventually written to the XML file
Myxmlwriter.close ();
Page.Response.Write ("Generate XML document successful!");
25}
Num Catch
27 {
Page.Response.Write ("Failed to generate XML document!) Verify that the path is correct and that there are write permissions. ");
29}
30}
31
32private void Writexmlbyxmlwriter (XmlWriter writer, string symbol, double, double change, long volume)
33{
Writer. WriteStartElement ("Stock");
Writer. WriteAttributeString ("symbol", symbol);
Writer. WriteElementString ("Price", xmlconvert.tostring (price));
Panax Notoginseng writer. WriteElementString ("Change", xmlconvert.tostring (change));
Writer. WriteElementString ("Volume", Xmlconvert.tostring (Volume));
Writer. WriteEndElement ();
40}

Two Reading XML files through the DOM


1/**////<summary>
2///reading XML files through the DOM
3///</summary>
4private void Readxmlbydom ()
5{
6/**////Create an instance of the XmlDocument class
7 XmlDocument doc = new XmlDocument ();
8 ArrayList nodevalues = new ArrayList ();
9
10/**////to read the People.xml file into memory, forming a DOM structure
Doc. Load (Server.MapPath ("People.xml"));
XmlNode root = Doc. DocumentElement;
A foreach (XmlNode personelement in root. ChildNodes)
Nodevalues.add (PersonElement.FirstChild.Value);
15
Xmlnodelistbox.datasource = nodevalues;
Xmlnodelistbox.databind ();
18}
19

Three Reading XML files through XmlReader


1/**////<summary>
2///read XML files through XmlReader
3///</summary>
4private void Readxmlbyxmlreader ()
5{
6/**////Create XML reader
7 XmlTextReader reader = new XmlTextReader (Server.MapPath ("Students.xml"));
8 ArrayList nodevalues = new ArrayList ();
9
Ten while reader. Read ())
11 {
The IF (reader. NodeType = = XmlNodeType.Element && reader. name = = "Name")
13 {
Reader. Read ();
Nodevalues.add (reader. Value);
16}
17}
18
Xmlnodelistbox.datasource = nodevalues;
Xmlnodelistbox.databind ();
21}

Four Reading XML files through XPath


1/**////<summary>
2///read XML files through XPath
3///</summary>
4private void Readxmlbyxpath ()
5{
6/**////Note: A System.XML.XPath namespace needs to be introduced
7 XPathDocument Xpdoc = new XPathDocument (Server.MapPath ("People.xml"));
8 XPathNavigator nav = Xpdoc. CreateNavigator ();
9 XPathExpression expr = Nav.compile ("descendant::P Eople/person");
Ten XPathNodeIterator iterator = nav. Select (expr);
One ArrayList nodevalues = new ArrayList ();
12
while (iterator. MoveNext ())
Nodevalues.add (iterator. Current.tostring ());
15
Xmlnodelistbox.datasource = nodevalues;
Xmlnodelistbox.databind ();
18}

Five displaying XML files through XSL


1/**////<summary>
2///XML file through XSL
3///</summary>
4private void Displayxml ()
5{
6 System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument ();
7 xmldoc. Load (Server.MapPath ("User.xml"));
8 System.Xml.Xsl.XslTransform Xmltrans = new System.Xml.Xsl.XslTransform ();
9 Xmltrans. Load (Server.MapPath ("user.xsl"));
Ten xml1.document = xmldoc;
One xml1.transform = Xmltrans;
12}

Six validating XML files


/**////<summary>
validating XML files
</summary>
private void Validatexml ()
{
FileStream stream = new FileStream (Server.MapPath ("People.xml"), FileMode.Open);

/**////create an object for the XmlValidatingReader class
XmlValidatingReader VR = new XmlValidatingReader (stream, xmlnodetype.element, null);

/**////Load XML Schema document
Vr. Schemas.add (NULL, Server.MapPath ("people.xsd"));

/**////describes how validation is based on the XML schema
Vr. ValidationType = ValidationType.Schema;
Vr. ValidationEventHandler + = new ValidationEventHandler (Validationhandler);

/**////Document Validation
while (VR. Read ());

/**////Display Verification Process complete
Page.Response.Write ("<b>validation finished!<b>");

/**////to close open files
Stream. Close ();
}

private void Validationhandler (object sender, ValidationEventArgs args)
{
/**////displays a message that the validation failed
Page.Response.Write ("<b>validation Error: </b>" + args.) Message + "<p>");
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.