Summary (6) - XML ​​file operations in ASP.NET

xiaoxiao2021-03-17  222

Summary (6) - XML ​​file operations in ASP.NET

Squire: Terrylee

One. Write XML file

1

/ ** /

///

2 /// Write XML file 3 ///

4

Private

Void

Writexml ()

5

{6 / ** //// Create a string that represents the XML file path to be generated. If the path points to the NTFS partition, relevant access rights are required. 7 string filename = XMLFILEPATHTEXTBOX.TEXT; 8 9 / ** //// Create a file stream written to XML data 10 System.IO.FileStream myfilestream = new system.io.filestream (filename, system.io.filemode.create ); 11 12 / ** //// stream object creates a file using an object XmlTextWriter 13 XmlTextWriter myXmlWriter = new XmlTextWriter (myFileStream, System.Text.Encoding.Unicode); 1415 myXmlWriter.Formatting = Formatting.Indented; 1617 try18 {19 / ** //// Use the WriteXmlbyXMLWRITER method to write data into the XMLTextWriter object 20 WriteXmlbyxmlwriter (MyXMLWriter, "MSFT", 74.5, 5.5, 49020000); 21 22 / ** //// The call of the Close method, XMLTextWriter object The data finally writes XML file 23 myxmlwriter.close (); 24 page.response.write ("Generate XML Document Success!"); 25} 26 catch27 {28 Page.Response.write ("Generate XML document failed! Please check Whether the path is correct, and whether there is write permission. "); 29} 30}

31

32

Private

Void

Writexmlbyxmlwriter (XMLWRITER WRITER,

String

Symbol,

Double

PRICE,

Double

Change,

Long

Volume)

33

{34 writer.WriteStartElement ( "Stock"); 35 writer.WriteAttributeString ( "Symbol", symbol); 36 writer.WriteElementString ( "Price", XmlConvert.ToString (price)); 37 writer.WriteElementString ( "Change", XmlConvert .Tostring (change)); 38 Writer.writeElementString ("Volume", Xmlconvert.toString (Volume)); 39 Writer.WriteEndelement (); 40}

two. Read XML files via DOM

1

/ ** /

///

2 /// Read XML file 3 //// via DOM

4

Private

Void

Readxmlbydom ()

5

{6 / ** //// Example 7 xmlDocument doc = new xmldocument (); 8 arraylist nodevalues ​​= new arraylist (); 9 10 / ** /// / read the people.xml file into memory, forming a DOM structure 11 doc.Load (Server.MapPath ( "people.xml")); 12 XmlNode root = doc.DocumentElement; 13 foreach (XmlNode personElement in root.ChildNodes) 14 NodeValues.Add (personElement.FirstChild.Value) ; 1516 xmlnodelistbox.datasource = nodevalues; 17 xmlnodelistbox.database (); 18} 19

three. Read XML files via XmlReader

1

/ ** /

///

2 /// Read XML file 3 /// via XMLReader

4

Private

Void

ReadxmlbyxmlReader ()

5

{6 / ** //// Create an XML reader 7 xmlTextReader reader = new xmlTextReader (Server.MAppath ("students.xml")); 8 arraylist nodevalues ​​= new arraylist (); 910 While (Reader.Read () 11 {12 if (reader.NodeType == xmlNodetype.Element && Reader.Name == "name") 13 {14 reader.read (); 15 nodeValues.add (reader.value); 16} 17} 1819 XMLNodelistbox. DataSource = NodeValues; 20 xmlnodelistbox.database (); 21}

four. Read XML files via XPath

1

/ ** /

///

2 /// Read XML file 3 //// via XPath

4

Private

Void

READXMLBYXPATH ()

5

{6 / ** //// Note: Need to introduce system.xml.xpath namespace 7 xpathdocument xpdoc = new xpathdocument (Server.MAppath ("people.xml")); 8 Xpathnavigator NAV = xpdoc.createnavigator (); 9 XPathExpression expr = nav.Compile ( "descendant :: PEOPLE / PERSON"); 10 XPathNodeIterator iterator = nav.Select (expr); 11 ArrayList NodeValues ​​= new ArrayList (); 1213 while (iterator.MoveNext ()) 14 NodeValues.Add (iTerator.current.toString ()); 1516 xmlnodelistbox.datasource = nodeValues; 17 xmlnodelistbox.DATABIND (); 18} Five. Display XML files via XSL

1

/ ** /

///

2 /// Display XML file 3 /// via XSL

4

Private

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")); 10 xml1.document = xmldoc; 11 xml1.transform = XmlTrans; 12}

six. Verify the XML file

/ ** /

///

/// Verify XML file ///

Private

Void

Validatexml ()

{FileStream stream = new FileStream (Server.MapPath ( "people.xml"), FileMode.Open); / ** //// create an object class XmlValidatingReader XmlValidatingReader vr = new XmlValidatingReader (stream, XmlNodeType.Element, null); / ** //// Load XML architecture document vr.schemas.add (null, server.mappath ("people.xsd"))); / ** /// Description Verification is based on XML architecture vr.validationType = ValidationType.Schema; vr.validationEventhandler = New ValidationEventHandler (ValidationHandler); / * /// // /////////// The verification process is displayed Page.Response. Write (" Validation Finished! "); / ** /// Off Opened file stream.close ();} private

Void

ValidationHandler

Object

Sender, ValidationEventArgs Args)

{/ ** /// / Display Verification Failed message Page.Response.write (" Validation Error: args.Message "

");}

转载请注明原文地址:https://www.9cbs.com/read-129651.html

New Post(0)