Read and write XML

Source: Internet
Author: User

This article is mainly about reading XML data into the database, and writing data from the read database to XML, the database using MySQL

First, the brain complements the knowledge of XML:

XML: The advantage of storing data: Each language has a built-in XML file analysis engine that does not have to be written separately from the file analysis engine. XML Syntax specification: 1) must be case-sensitive 2) document can only have one root node 3) attribute value must be in double quotation marks 4) have start tag must have end tag 5) document declaration: <?xml version= "1.0" encoding= "Utf-8"?> Read data from the database written to XML: Setattributevalue and Setelementvalue when used primarily in the method
1 Static voidMain (string[] args)2         {3DataTable table =getdatatable ();4List<student> stulist =tabletolist (table);5XDocument Xdoc =NewXDocument ();6XElement root =NewXElement (" Person");7Xdoc. ADD (root);//Add root node8             foreach(Student Stuinchstulist)9             {TenXElement Stuele =NewXElement ("Student"); OneStuele.setattributevalue ("Stuid", Stu. Student_id.tostring ());//Setting Properties AStuele.setelementvalue ("name", Stu. Name);//set child nodes and values -Stuele.setelementvalue ("class", Stu. Class); -Stuele.setelementvalue ("Gender", Stu. Gender); the Root. ADD (Stuele); -             } -Xdoc. Save ("1.xml"); -Console.WriteLine ("Database Write Complete"); + Console.readkey (); -         } +         /// <summary> A         ///reading database table Data at         /// </summary> -         /// <returns></returns> -          Public StaticDataTable getdatatable () -         { -             stringConnStr = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; -DataSet ds= Mysqlhelper.executedataset (CONNSTR,"SELECT * FROM Student"); in            returnDs. tables[0]; -         } to         /// <summary> +         ///convert a row to a list -         /// </summary> the         /// <param name= "table" ></param> *         /// <returns></returns> $          Public StaticList<student>tabletolist (DataTable table)Panax Notoginseng         { -list<student> list =NewList<student>(); the              for(inti =0; I < table. Rows.Count; i++) +             { AStudent stu =NewStudent (); theDataRow row=table. Rows[i]; +Stu. Class = row["Class"]. ToString (); -Stu. Gender = row["Gender"]. ToString (); $Stu. Name = row["Name"]. ToString (); $Stu. student_id = Convert.ToInt32 (row["student_id"]); - list. ADD (Stu); -             } the             returnlist; -}

Reading XML data into the database

The main method to use is item. Attribute ("Xname"). value--gets the value of the property of item. Element ("Xname"). value--gets the value of the child node

1            stringConnStr ="Server=localhost;database=test;user id=root;password=123";2XDocument Xdoc = Xdocument.load (@"F:\tmp\1.xml");3XElement root =Xdoc. Root;4             foreach(XElement IteminchRoot. Elements ())5             {6                 //Console.WriteLine (item. Element ("name"). Value);7                 //Console.WriteLine (item. Element ("Class"). Value);8                 //Console.WriteLine (item. Element ("Gender"). Value);9                 stringsql ="INSERT into student (Student_id,name,class,gender) VALUES (@Student_Id, @Name, @Class, @Gender)";TenMysqlparameter[] PS = {  One                                       NewMysqlparameter ("@Student_Id", item. Attribute ("Stuid"). Value),//Reading property values A                                       NewMysqlparameter ("@Name", item. Element ("name"). Value),//read child node values -                                       NewMysqlparameter ("@Class", item. Element ("class"). Value), -                                       NewMysqlparameter ("@Gender", item. Element ("Gender"). Value) the                                       }; -Mysqlhelper.executenonquery (CONNSTR, SQL, PS);//Insert Database -             } -Console.WriteLine ("OK"); +Console.readkey ();

Read and write XML

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.