XMl Write file method (summarized according to Multi-person-tracking article)

Source: Internet
Author: User



XmlTextWriter write a file, the default is to overwrite the previous file, if the file name does not exist, it will create this file, first create the XML file format


1.

XmlTextWriter myXmlTextWriter = new XmlTextWriter (".. \.. \xxx.xml ", NULL);


2. Use the Formatting property to specify what format you want to set the XML to. Elements like this can be indented by using the indentation and IndentChar properties.


myXmlTextWriter.Formatting = formatting.indented;


The elements can then be created using the WriteStartElement and WriteElementString methods, the difference being that if there are elements of a child node, then it is created with writestartelement and then to create the element

Once created, call the appropriate writeendelement to tell the compiler, create a single element with writeelementstring, and create the attribute with WriteAttributeString. as follows:


   1:xmltextwriter myXmlTextWriter = new XmlTextWriter (@ "... \..
   \book1.xml ", NULL); 2://Use the Formatting property to specify what format you want to set the XML to.
   In this way, child elements can be indented by using the indentation and IndentChar properties.
   3:myxmltextwriter.formatting = formatting.indented;
   4:5: Myxmltextwriter.writestartdocument (FALSE);
   6:myxmltextwriter.writestartelement ("bookstore");
   7:8: myXmlTextWriter.WriteComment ("Information for recording books");
  9:myxmltextwriter.writestartelement ("book");
  10:11:myxmltextwriter.writeattributestring ("Type", "elective");
  12:myxmltextwriter.writeattributestring ("ISBN", "111111111");
  13:14:myxmltextwriter.writeelementstring ("Author", "Zhang San");
  15:myxmltextwriter.writeelementstring ("title", "career Plan");
  16:myxmltextwriter.writeelementstring ("Price", "16.00");
  17:18:myxmltextwriter.writeendelement ();
  19:myxmltextwriter.writeendelement (); 20: 
  21:myxmltextwriter.flush (); 22:myxmltextwriter.close ();


Raise:

WriteStartDocument () method


WriteStartDocument becomes the writer validation, and you are writing a well-formed XML document. For example, he checks that the XML declaration is the first node, and that only one root-level element exists, and so on.

If this method is not called, the writer assumes that an XML fragment is being written and does not apply any root-level rules.







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.