usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespacexmltest{classProgram {//first scenario XML data Flow Static voidReadorder () {xmlreadersettings settings=NewXmlReaderSettings ();//control parsing and validation processesSettings. Ignorewhitespace =true;//Ignore blank areasSettings. Ignorecomments =true;//Ignore CommentsSettings. ConformanceLevel = conformancelevel.document;//whether to have only one root node using(XmlReader reader = Xmlreader.create ("Sample.xml", settings))//Open File { while(Reader. Read ())//The XML stream unit is a XMNL node and is unidirectional, and each call to read () causes the node to point to the next position, or false to indicate the end of the file{Console.WriteLine (New string(' ', reader. Depth *2));//DepthConsole.WriteLine (reader. NodeType);//node type, reference XmlNodeType structure, Switch(reader. NodeType) { CaseXmlNodeType.Element://element (for example,,<item>). CaseXmlNodeType.EndElement://The end element tag (for example,,</item>). Console.WriteLine (reader. Name); Break; CaseXmlNodeType.Text://the text content of the node. CaseXmlnodetype.comment://comments (such as,<!--my comment--). CaseXmlnodetype.attribute://PropertiesConsole.WriteLine (reader. Value); Break; } Console.WriteLine ("================================"); } } } Static voidreadtest () {xmlreadersettings settings=Newxmlreadersettings (); Settings. Ignorewhitespace=true; using(XmlReader reader = Xmlreader.create ("Sample.xml", Settings)) { stringstr =""; //checks whether the current node is a node of content (non-whitespace text, CDATA, Element, EndElement, EntityReference, or endentity), and if the node is not a content node, the reader jumps forward to the next content node or end of the file. It skips the following types of nodes: ProcessingInstruction, DocumentType, Comment, whitespace, or significantwhitespace. Reader. MoveToContent (); //checks whether the current content node is an element with a given System.Xml.XmlReader.Name and advances the reader to the next node. Reader. Readstartelement ("Openni"); Console.WriteLine (reader["ID"]); //When overridden in a derived class, moves to the property that has the specified System.Xml.XmlReader.Name. Reader. MoveToAttribute ("name"); //reads the text content of the current position as a System.String object. Console.WriteLine (reader. Readcontentasstring ()); Reader. MoveToAttribute ("ID"); Console.WriteLine (reader. Readcontentasstring ()); //after the properties are read, the nodes are located in attribute and can be processed by viewing the Notetype as follows://The first way://Reader. Readstartelement (); //str = reader. Readelementcontentasstring ("Price", ""); //Console.WriteLine (str); //Reader. Readendelement (); //The second way://When overridden in a derived class, moves to the element that contains the current attribute node. Reader. Movetoelement (); STR= Reader. Readelementcontentasstring ("title",""); Console.WriteLine (str); Reader. Readstartelement ("Licenses"); //checks whether the specified local name and namespace URI match the local name and namespace URI of the current element, and then reads the current element and takes the content as a System.Stringstr = reader. Readelementcontentasstring (" Price",""); Console.WriteLine (str); Reader. Readendelement (); Reader. Readendelement (); } } Static voidWritetest (stringfilename) {xmlwritersettings Settings=Newxmlwritersettings (); Settings. Indent=true; Settings. Omitxmldeclaration=false; using(XmlWriter writer =xmlwriter.create (filename, settings)) { //starting ElementWriter. WriteStartElement (" Book"); //Write PropertiesWriter. WriteAttributeString ("name","The Art of code"); Writer. WriteAttributeString (" Time", DateTime.Now.ToString ()); //Node 1Writer. WriteElementString ("name","The Art of code"); //Node 2Writer. WriteStartElement (" Time"); Writer. WriteValue (DateTime.Now); Writer. WriteEndElement (); //End "book" Nodewriter. WriteEndElement (); } } //========================================================== Second Solution Static voidWritexmldoc (String filename) {XmlDocument XML=NewXmlDocument (); //Add a claimXmlDeclaration xmldecl = XML. Createxmldeclaration ("1.0","gb2312",NULL); Xml. AppendChild (XMLDECL); //Creating the root nodeXmlElement root = XML. CreateElement ("Root"); Xml. AppendChild (root); //Creating PropertiesXmlAttribute id = XML. CreateAttribute ("ID"); Id. Value="1234"; //Create Time PropertiesXmlAttribute time = XML. CreateAttribute (" Time"); Time. Value=DateTime.Now.ToString (); //Creating ElementsXmlElement name = XML. CreateElement ("name"); Name. AppendChild (XML. createTextNode ("The Art of code")); XmlElement author= XML. CreateElement ("author"); Author. AppendChild (XML. createTextNode ("s.b")); XmlElement Book= XML. CreateElement (" Book"); Book. AppendChild (name); Book. AppendChild (author); //root node Add propertyRoot. Attributes.append (ID); Root. Attributes.append (time); //Setting PropertiesRoot. SetAttribute ("Num","10000"); //Add child nodes to a nodeRoot. AppendChild (book); Xml. Save (filename); } Static voidReadxmldoc (stringfilename) {XmlDocument XML=NewXmlDocument (); Xml. Load (filename); //Get PropertiesConsole.WriteLine (XML. documentelement.attributes[" Time"]. Value); //Traversing child nodes if(XML. Documentelement.haschildnodes) {XmlNodeList list=XML. Documentelement.childnodes; foreach(XmlNode itinchlist) {Console.WriteLine (it. INNERXML); } } } Static voidMain (string[] args) { //Readorder (); //readtest ();//writetest ("Test.xml");Writexmldoc ("Doc.xml"); Readxmldoc ("Doc.xml"); Console.read (); } }}Drizzle mark: drizzle
C # XML file operations, explanation see comments