In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to write XML in .NET", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to write XML in .NET.
Xml is a popular technology. One of the main reasons why it can arouse people's interest is that it is very simple and people can easily understand and use it. Every programmer can easily read a XML file and understand what it contains.
.net contains a number of classes that support XML, which make programming with XML as easy as understanding XML files. In this article, I will give an example of the use of such a class, which is the XmlTextWriter class.
The XmlTextWriter class allows you to write XML to a file. This class contains a number of methods and properties that make it easier for you to deal with XML. In order to use this class, you must first create a new XmlTextWriter object, and then you can add XML fragments to the object. This class contains a number of methods for adding various types of XML elements to the XML file, and the following table gives the names and descriptions of these methods:
Method
Description
WriteStartDocument
Write a XML statement with version "1.0"
WriteEndDocument
Close any open elements or attributes
Close
Close the stream
WriteDocType
Write a DOCTYPE declaration with the specified name and optional attributes
WriteStartElement
Write out the specified opening tag
WriteEndElement
Close an element
WriteFullEndElement
Close an element and always write the full closing tag
WriteElementString
Write out the element containing the string value
WriteStartAttribute
The starting content of the writing attribute
WriteEndAttribute
Close the last WriteStartAttribute call
WriteRaw
Write the original mark manually
WriteString
Write a string
WriteAttributeString
Output an attribute with a specified value
WriteCData
Write out the block containing the specified text
WriteComment
Write out a comment containing the specified text
WriteWhiteSpace
Write out the given white space
WritePRocessingInstruction
Write a processing instruction with a space between the name and the text, as follows:
If you are familiar with XML, you must have a good understanding of the above methods. Next we will give an example, in which we will first create a document, add some elements, and then close the document. After adding elements, you can also add child elements, attributes and other content. The following code is an example of this, which creates a XML file named title.
Using System;using System.IO;using System.Xml;public class Sample {public static void Main () {XmlTextWriter writer = new XmlTextWriter ("titles.xml", null); / / write the root element writer.WriteStartElement ("items"); / / add child elements writer.WriteElementString ("title", "Unreal Tournament 2003"); writer.WriteElementString ("title", "ClearC: Renegade") Writer.WriteElementString ("title", "Dr. Seuss's ABC"); / / close the root element and write the closing tag writer.WriteEndElement (); / / write XML to the file and close XmlTextWriter writer.Close ();}}
If you compile and execute the above code, you will create the XML file that contains the following:
Unreal Tournament 2003 C: Renegade Dr. Seuss's ABC
The above code creates a XmlTextWriter object named writer. When this object is created, it is associated with a file called titles.xml. Next, the program creates a root attribute called items, and the WriteStartElement method creates the opening tag for this attribute. Next, the program calls the WriteElementString method to create three child elements. You can also see from the above code that this method uses the first parameter (title in the above program) as the tag of the element and the second parameter as the value of the element. When you have added all the elements, you need to close the root element. You can then call the WriteEndElement method to close the recently opened element; in this case, the recently opened element is the root element. When all the data has been written and the root element has been closed, you can send the information to your XmlTextWriter. This means that at this point you can call the Close method to close it.
The above code is relatively simple. Let's take a look at an example that uses more methods in the XmlTextWriter class and is more functional.
Using System;using System.IO;using System.Xml;public class Sample {public static void Main () {XmlTextWriter writer = new XmlTextWriter ("myMedia.xml", null); / / use automatic indentation to make it easy to read writer.Formatting = Formatting.Indented; / / write the root element writer.WriteStartElement ("items"); / / start an element writer.WriteStartElement ("item") / / add an attribute writer.WriteAttributeString ("rating", "R") to the previously created element; / / add child elements writer.WriteElementString ("title", "The Matrix"); writer.WriteElementString ("format", "DVD"); / / close the item element writer.WriteEndElement (); / / close the element / / add some spaces between nodes writer.WriteWhitespace ("\ n") / / write the second element writer.WriteRaw ("" + "BloodWake" + "XBox" + "") using the original string / / write the third element writer.WriteRaw ("\ n\ n" + "Unreal Tournament 2003\ n" + "CD\ n" + "\ n") using a formatted string; / / close the root element writer.WriteFullEndElement () / / write XML to the file and close writer writer.Close ();}}
After the above code is compiled and run, you will get the myMedia.xml file, which contains:
The Matrix DVD BloodWake XBox Unreal Tournament 2003 CD
The comments in the above code show how the function of this program is implemented. One thing to keep in mind is that when calling a method to start an operation, you need to call the method to end the operation in the right place in the program. For example, if you call StartElement, you must call EndElement to close the element; of course, you can add a child element between the two calls. Whenever you call the EndElement method, it always closes the element that was recently opened with the StartElement method (much like the way the stack works).
It's easy to use XmlTextWriter, but I suggest you try these codes and methods yourself. After you try it, you will find that this code can be easily integrated into your program. You should also keep in mind that XmlTextWriter is just one of the many XML classes provided by .NET. Like XmlTextWriter, other XML classes are very easy to use
These are all the contents of the article "how to write XML in .NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.