Compile XML using XML in Visual C #

Source: Internet
Author: User

Today, I will talk about how to write XML documents in C #. At first, I thought it would be a bit of a headache to write XML in programming mode. Later I thought it was useful, I think Microsoft's team should be able to compile these classes not just to deliver to bill I gates! As for its usefulness ...... For example, installProgramAh! We can generate corresponding XML documents based on the options and some settings selected during the installation process, and then initialize our applications based on the XML documents. If it is empty, let's take a look at the specific implementation details.
To write XML, we also use the stream concept. the XML compilation details in net are implemented as the xmlwriter class, but this class is an abstract class that cannot be instantiated. Therefore, we need to access the method in the program to implement the desire to write XML, you must use its derived class xmltextwriter. This class provides a series of attributes and methods to prepare for the compilation of XML. The following describes the class in detail:

 

Constructor:

 

 
PublicXmltextwriter (textwriter );PublicXmltextwriter (stream, encoding );PublicXmltextwriter (String, Encoding );

The first constructor is to pass the existing textwriter instance over. The system. Io. textwriter class is an ordered response stream.
The second constructor uses the stream to be written as the first parameter. The second parameter specifies the encoding method of the XML document. The default value is utf8. encoding can be used as the enumerated value, stream can be filestream, memorystream, networkstream, etc.
The third constructor is to pass the file name to be written as a string (if the object exists, rewrite the object) to the first parameter, and the second parameter specifies the encoding method.
Common Methods:
Writerstartdocument () and writerenddocument () methods:
The first method is used to compile the XML declaration section, for example: <? XML version = "1.0" encoding = "UTF-8"?>
The second method is used to disable any open elements or attributes and reset the writer to the start state.
Writerstartelement () and writeendelement () methods:
The first method is used to write the specified start Mark. This method has the following reloads:

 

 
Writerstartelement (StringLocalname)

Use the passed string as the local name of the element

 

 
Writerstartelement (StringLocalname,String Namespace)

The first parameter specifies the local name of the element, and the second parameter specifies the namespace of the element.

 

 
Writerstartelement (StringPrefix,StringLocalname,String Namespace)

The first parameter specifies the prefix of the element, the second parameter specifies the local name of the element, and the third parameter specifies the namespace of the element.
The second method is used to write the close element corresponding to the Start Element. If the start element does not contain any content, a "/>" is used as the close element.
Writerstartattribute () and writerendattribute () methods:
The first method is used to compile the beginning of an attribute. This method has two reloads:

 
Writerstartattribute (StringLocalname,String Namespace)

The first parameter specifies the local name of the attribute, and the second parameter specifies the namespace of the attribute.

 
Writerstartattribute (StringPrefix,StringLocalname,String Namespace)

The first parameter specifies the prefix of the attribute, the second parameter specifies the local name of the attribute, and the third parameter specifies the namespace of the attribute.
The second method is used to disable the properties created by writerstartattribute.
Writerelementstring () method:
This method creates an element that contains a string value, which has the following overloading:

 
Writerelementstring (StringLocalname,String Value)

If you writeCode:

 
Writeelementstring ("Para","Some text")

Output

 
<Para> some text </para>

 

 
Writerelementstring (StringLocalname,String Namespace,String Value)

If you write such code:

 
Writerelementstring ("Para",Http://www.w3.org/ns","Some text")

Output:

 
<Para xmlns = "http:// Www.w3.org/ns”> some text </para>

You can use the writerstartelement () and writerendelement () Methods to compile nested elements. You can use this method to compile elements that directly contain content.
Writerattributestring () method:
Similar to the writerelementstring () method, if the attribute value does not contain an entity, you can use this method to write the attribute directly. If the attribute value contains an object, you can use writerstartattribute () and writerendattribute () method, for example, to write such XML -- <para author = "do & 0241; A & amp; L. perez "/>, you can write the following code:

 

Writerstartelement ("para"); writerstartattribute ("author ",Null); Writerstring ("do"); writercharentiry ("~ N "); writerstring (" A "); writercharentiry (" & "); writerstring (" L. Perez "); writerendattriement (); writerendelement ();

This method has the following overload:

 

 writerattributestring ( string  localname,  string   value  ); writerattributestring ( string  localname,  string   namespace ,  string   value  ); writerattributestring ( string  prefx,  string  localname,  string   namespace ,  string   value ); 

Writernode (xmlreader reader, bool defattr) method:
This method can copy nodes from the xmlreader reader and write them into the xmlwriter stream. The first parameter is the xmlreader instance, and the second parameter accepts a Boolean value to determine whether to copy the attributes in the element, consider the following XML snippet:

 

 
<Para> <sent> the <B> xmlwriter </B>ClassWrites XML content to a stream. </sent> </para>

The following code copies the snippets. Reader represents the xmlreader instance writer represents the xmlwriter class instance:

 

While(Reader. Read ()){If(Reader. Name = "sent" & reader. nodetype = xmlnodetype. element) {writer. writernode (reader,True);}}

The following output is displayed:

 

<Sent> the <B> xmlwriter </B>ClassWrites XML content to a stream. </sent>

writercomment (string text) method: used to write a comment
writerstring (string text) method: used to write a text
writercdata (string text) method: write CDATA data blocks
writerbase64 (byte [] buffer, int index, int count) method: encode the specified binary bytes into base64 and write the result text
flush (): refresh all content in the buffer to the base stream, and refresh the base stream. Close (): close this stream and the base stream .

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.