1 classXmlclass2 {3 Public stringPathname;//Total List4 PublicXmlclass ()5 {6 stringstr =System.AppDomain.CurrentDomain.BaseDirectory;7 stringPath =str;8 9 for(inti =0; I <3; i++)Ten { One stringRootPath = path. Substring (0, Path. LastIndexOf ("\\")); APath =RootPath; - } -Pathname = path +"\\Document\\myxml.xml"; the } - - Public string[] Xmlstore () - { + string[]elements =New string[2]; -XmlReader reader =xmlreader.create (Pathname); + A while(reader. Read ()) at { - if(Reader. NodeType = = XmlNodeType.Element)//If it is the start node - { - Switch(reader. Name) - { - Case "Meet": in { - if(Reader. Value = ="") toelements[0] = (string) reader. ReadElementContentAs (typeof(string),NULL);
//Apply parent node contains a single child element, otherwise the remaining child elements cannot get + Else -elements[0] =Reader. Readcontentasstring (); the} Break; * $ Case "Topic":Panax Notoginseng { - if(Reader. Value = ="") theelements[1] = (string) reader. ReadElementContentAs (typeof(string),NULL); + Else Aelements[1] =Reader. Readcontentasstring (); the} Break; + default: - Break; $ } $ } - } - Reader. Close (); the Reader. Dispose (); - returnelements;Wuyi } the - Public voidXmldisplay (stringMetting,stringtopic) Wu { - using(FileStream fs =NewFileStream (Pathname, FileMode.Create, FileAccess.Write)) About { $XmlWriter writer =xmlwriter.create (FS); -Writer. WriteStartElement ("Root"); - -Writer. WriteStartElement ("Root1"); AWriter. WriteElementString ("Meet", metting); + writer. WriteEndElement (); the -Writer. WriteStartElement ("Root2"); $Writer. WriteElementString ("Topic", topic); the writer. WriteEndElement (); the writer. WriteEndElement (); the writer. Close (); the writer. Dispose (); - } in } the}
C # XML Read and write