Create an XML file and save the file->xml

Source: Internet
Author: User
Tags truncated

Not used in C # before, because in the internship unit use of the database, need to use C #, so learning some, is full of fur, slightly write, right when summarized and recorded.

I want to use, through the program to implement data storage, and stored in the XML document, the beginning of the tutorial, all useless, reading does not practice, equal to the white school.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespacecreatexml{classProgram {Static voidMain (string[] args) {Program App=NewProgram (); App.        Createxmlfile (); }         Public voidCreatexmlfile () {XmlDocument xmldoc=NewXmlDocument (); //creating a type declaration node//XmlNode node=xmldoc.createxmldeclaration ("1.0", "Utf-8", "" "); //xmldoc.appendchild (node); //Creating the root nodeXmlNode Root= Xmldoc.createelement ("annotation");            Xmldoc.appendchild (root); CreateNode (Xmldoc,root,"folder","VOC2007"); CreateNode (xmldoc, Root,"filename","000001.jpg"); XmlNode Node3= Xmldoc.createnode (XmlNodeType.Element,"Source",NULL); CreateNode (xmldoc, Node3,"Database","The VOC2007 Database"); CreateNode (xmldoc, Node3,"annotation","pacal VOC2007"); CreateNode (xmldoc, Node3,"Image","Flickr"); CreateNode (xmldoc, Node3,"Flickrid","341012865"); Root.            AppendChild (NODE3); XmlNode Node4= Xmldoc.createnode (XmlNodeType.Element,"owner",NULL); CreateNode (xmldoc, Node4,"Flickrid","Fried Camels"); CreateNode (xmldoc, Node4,"name","Jinky the Fruit Bat"); Root.            AppendChild (NODE4); XmlNode Node5= Xmldoc.createnode (XmlNodeType.Element,"size",NULL); CreateNode (xmldoc, NODE5,"width","353"); CreateNode (xmldoc, NODE5,"Heigh"," -"); CreateNode (xmldoc, NODE5,"Depth","3"); Root.            AppendChild (NODE5); CreateNode (xmldoc, Root,"Segmented","0"); //more than one layer of nodesXmlNode Node7 = Xmldoc.createnode (XmlNodeType.Element,"Object",NULL); CreateNode (xmldoc, Node7,"name","Dog"); CreateNode (xmldoc, Node7,"pose"," Left"); CreateNode (xmldoc, Node7,"truncated","1"); CreateNode (xmldoc, Node7,"Difficult","0"); XmlNode Node70= Xmldoc.createnode (XmlNodeType.Element,"Obndbox",NULL); CreateNode (xmldoc, Node70,"xmin"," -"); CreateNode (xmldoc, Node70,"ymin"," -"); CreateNode (Xmldoc,node70,"Xmax","195"); CreateNode (xmldoc, Node70,"Ymax","371"); Node7.            AppendChild (NODE70); Root.            AppendChild (NODE7); XmlNode Node8= Xmldoc.createnode (XmlNodeType.Element,"Object",NULL); CreateNode (xmldoc, Node8,"name"," Person"); CreateNode (xmldoc, Node8,"pose"," Left"); CreateNode (xmldoc, Node8,"truncated","1"); CreateNode (xmldoc, Node8,"Difficult","0"); XmlNode Node80= Xmldoc.createnode (XmlNodeType.Element,"Object",NULL); CreateNode (xmldoc, Node80,"xmin","8"); CreateNode (xmldoc, Node80,"ymin"," A"); CreateNode (xmldoc, Node80,"Xmax","352"); CreateNode (xmldoc, Node80,"Ymax","498"); Node8.            AppendChild (NODE80); Root.            AppendChild (NODE8); Try{Xmldoc.save ("c:/users/'/desktop/my.xml");//Save Path            }            Catch(Exception e) {//Display error messageConsole.WriteLine (e.message); }            //console.readline ();        }        /// <summary>        ///Create a node/// </summary>        /// <param name= "xmldoc" ></param>XML Document/// <param name= "parentnode" ></param>parent Node/// <param name= "name" ></param>Node name/// <param name= "value" ></param>Node Value///         Public voidCreateNode (XmlDocument xmldoc, XmlNode parentnode,stringNamestringvalue) {XmlNode node= Xmldoc.createnode (XmlNodeType.Element, name,NULL); Node. InnerText=value;        Parentnode.appendchild (node); }    }}

Final Build:

<annotation> <folder>VOC2007</folder> <filename>000001.jpg</filename> <source> <database>the VOC2007 database</database> <annotation>pacal Voc2007</annotation> <image>flickr</image> <flickrid>341012865</flickrid> </source> <owner> <flickrid>fried camels</flickrid> <name>jinky th E Fruit bat</name> </owner> <size> <width>353</width>  -3</depth> </size> <segmented>0</segmented> <Object> <name>dog</name> <pose>Left</pose> <truncated>1</truncated> <difficult>0</difficult> <obndbox> <xmin> -</xmin> <ymin> -</ymin> <xmax>195</xmax> <ymax>371</ymax> </obndbox> </Object> <Object> <name>person</name> <pose>Left</pose> <truncated>1</truncated> <difficult>0</difficult> <Object> <xmin>8</xmin> <ymin> A</ymin> <xmax>352</xmax> <ymax>498</ymax> </Object> </Object></annotation>

Create an XML file and save the file->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.