About the XML file format provided by the. NET Framework

Source: Internet
Author: User
Xml

Always used Java, the processing of XML most of the time is directly using the JDK provided by the standard DOM and sax APIs, very little to care about the specific implementation of the API, personal preference for xerces, long time, accustomed to, nothing more.

Now, with. NET, the ubiquity of XML technology, it takes a little time to understand and adapt to the XML manipulation APIs provided by the. NET Framework. The. NET Framework provides no other API than Dom and sax, Only developed on the basis of traditional sax, what makes an improved sax that uses the "pull" technique is that it's really simpler and more flexible than traditional sax, and that you don't have to inherit one of those bizarre classes and realize what's troubling callback methods, Just get a while loop to embed a switch structure to "care only about the node type and node content that you care about". The classes that correspond to the DOM are XmlDocument and their derived classes, and the classes corresponding to sax are XmlReader and their derived classes, such as commonly used XmlTextReader.

The first two days to do a small tool, the role is based on the XML file description to batch generation of some 烦冗 complex boring C # code, the former teachers boast of the magic of XML mentioned "... Even you can use XML to implement a set of your own language ... "So, then listen not to take the matter, now want to really reputation (btw, in fact, similar things people xdoclet long ago do so). There is a small idea, that is, a messy XML text to make it beautiful, and then show to look comfortable, naturally think of XML document formatting.

It's easy to sort and beautify an XML document in the Java realm, and there are a lot of open source components that can do it brilliantly, but it really bothers me when using the. NET Framework API. It was only discovered that XmlTextWriter can do this for half a day, and XmlTextWriter must also output the content to a physical file or a stream (where you go:) unless you hand-piece the string. But I do not want to output to any place outside of memory and do not want to put together a boring string, and then go to the System.IO namespace, after a long time to find a MemoryStream (Java field does not seem to say this?) , although it is not informed but at least found the right. Fiddling with a bit, code posted, who would like to refer to it, there are good criticisms of the proposal can not be stingy to give me feedback.

private void Btnshow_click (object sender, System.EventArgs e)
{
string xmlfile = @ "C:\tmp.xml";

MemoryStream mstream = new MemoryStream (1024);
XmlTextWriter writer = new XmlTextWriter (mstream, NULL);
XmlDocument xmldoc = new XmlDocument ();
Writer. formatting = formatting.indented;

XmlDoc. Load (xmlfile);
XmlDoc. WriteTo (writer);
Writer. Flush ();
Writer. Close ();

Encoding Encoding = encoding.getencoding ("Utf-8");
This.rtxtResult.Text = Encoding. GetString (Mstream. ToArray ());
Mstream. Close ();
}

Note: This is just sample code and does not apply to industrial strength.

PostScript: At that time is using RichTextBox to display the XML text, although after the format, but still look uncomfortable, and then simply whole a webbrowser affixed to the form, directly to navigate an XML physical file, the effect is the same as with IE Open XML , indentation, highlighting, folding all have, worry and labor-saving. -_-#

Author: source

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.