How do I format the XML of a string?

Source: Internet
Author: User

The standard form of the "Ask" XML is:

<Root>

<Name>John</Name>

<Age>16</Age>

</Root>

Now if it's a string form--

<Root><Name>John</Name><Age>16</Age></Root>

How do I format the string so that it becomes the standard XML file format?

"Wrong answer."

Use the XmlDocument Loadxml method directly, and then save the output to a new XML document.

"Positive Solution"

XmlDocument is a. NET basic type that stores and operates on XML files. The XML file can be formatted directly in addition to the XML. XmlDocument with a method "WriteTo", the method requires a subclass that inherits the TextWriter class. For XML files, you should choose Xmltextwriter;xmltextwriter and need a StringWriter stream, and StringWriter need a stream object. It happens that StringBuilder is a subclass that implements the stream abstract class. So it can be used. The code is as follows:

[C #]

New XmlDocument ();
Doc. LOADXML ("<Root><Name>John</Name><Age>16</Age></Root> ");
New StringBuilder ();
New StringWriter (sub);
New XmlTextWriter (SW);
Xw. formatting = formatting.indented;
Doc. WriteTo (XW);
Console.WriteLine (sub);

[VB.net]

 dim  doc as  new  XmlDocument () 
Doc. LOADXML ( <root><name>john</ Name><age>16</age></root> )
dim [sub ] as new StringBuilder ()
dim SW as new StringWriter ([sub ])
Span style= "color: #0000ff;" >dim xw new XmlTextWriter (SW)
XW. formatting = formatting.indented
Doc. WriteTo (XW)
Console.WriteLine ([sub ])

It is important to note that you must set the XmlTextWriter formatting format to indented (jagged, auto indent), otherwise you will still not be able to achieve the effect.

Summary

1 ) first declare a StringBuilder object as the target source.

2 ) second, declare a StringWriter as the "pipe" (string "write stream") that connects to the object.

3 ) and then declare a XmlTextWriter , connect StringWriter (Put StringWriter passed in as a parameter) and set the Formatting format is indented .

4 ) finally through XmlDocument of the WriteTo method to connect to XmlTextWriter can be.

Expand

On "Stream" and "IO" of "Reader/writer"

It is well known that the System.IO namespace includes a large number of classes supporting files, memory, networks, strings, etc., and its methods. It derives mainly from several abstract classes:

1) Stream: This class is used primarily as a target object. In general, Microsoft's name is: "Target Object" +stream--

File stream: FileStream.

Memory stream: MemoryStream.

Network flow: NetworkStream (It is worth noting that there is a class that is located in System.NET.Sockets, which is used specifically when sending messages using Sockets or TCP/IP communications).

2) TextWriter: This class is used to perform a write operation that writes to a subclass that specifies an inherited stream class. Microsoft has a corresponding class in this regard:

file, memory, network stream write: StreamWriter.

String Stream writes: StringWriter (note: it requires a StringBuilder instead of a stream subclass object as an argument).

XML file stream write: XmlWriter and XmlTextWriter, the former is an abstract class (not inherited TextWriter), can also be used alone (using the Create method, support stream and StringBuilder, etc.), Simple implementation of some very simple basic XML read and write operations (mainly for string format), the latter inherited the XmlWriter (constructor parameters support the stream and the TextWriter subclass write), the implementation of all the abstract methods, Capable of reading and writing complex data types, such as CDATA, etc.). It also supports formatting formatting, just like the example above.

However, it is interesting to note that all of the above classes (end in writer) must explicitly call the close or flush method if they are written to the target, otherwise this will reside in memory and not be actually written to the target. This is why many times there is no error in the program, the goal is always not get the reason.

3) TextReader: This class is used to perform a "read" operation and read into a subclass that specifies an inherited stream class. Microsoft has a corresponding class in this regard:

file, memory, network stream read-in: StreamReader.

String stream read in: StringReader (note: It requires a string object instead of a stream subclass object as an argument).

XML file stream reads: XmlReader and XmlTextReader, the former is an abstract class (not inherited TextReader), can be used alone (using the Create method, support TextReader and stream, etc.), Simple implementation of some very simple basic XML read and write operations (mainly for the string format), the latter inherited the XmlWriter, the implementation of all the abstract methods, to read and write complex data types, etc.).

How do I format the XML of a string?

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.