In previous chapters we have dealt with reading XML, but now it's time to start writing it as well. Since XML is simply text, you could just start writing out XML tags to a file and give it an xml extension, but it's more easy and safer to let the .NET framework handle it, and just like with reading XML, there are at least two different options: The XmlWriter approach and the XmlDocument approach. This article will focus on the first approach, and then we'll look into writing XML with the XmlDocument in the next chapter.
The difference between the two is once again mostly related to memory consumption - XmlWriter uses less memory than XmlDocument, which is only an issue if you write very big files though. Another important difference is that when using XmlDocument, you can read an existing file, manipulate it and then write back the changes. With XmlWriter, you will have to write the entire document from scratch each time. This is not necessarily a problem though, so as always, it really comes down to your situation as well as your personal preferences.
Here's an example of writing XML using the XmlWriter class:
using System; using System.Text; using System.Xml; namespace WritingXml { class Program { static void Main(string[] args) { XmlWriter xmlWriter = XmlWriter.Create("test.xml"); xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("users"); xmlWriter.WriteStartElement("user"); xmlWriter.WriteAttributeString("age", "42"); xmlWriter.WriteString("John Doe"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("user"); xmlWriter.WriteAttributeString("age", "39"); xmlWriter.WriteString("Jane Doe"); xmlWriter.WriteEndDocument(); xmlWriter.Close(); } } }
The above code will create the following XML:
<users> <user age="42">John Doe</user> <user age="39">Jane Doe</user> </users>
So, we start by creating an instance of the XmlWriter class. It takes at least one parameter, in this case the path to where the XML should be written, but comes in many variations for various purposes. The first thing we should do is to call the WriteStartDocument() method. After that, we write a start element called "users". The XmlWriter will translate this into <users>. Before closing it, we write another start element, "user", which will then become a child of "users". We then proceed to adding an attribute (age) to the element, using the WriteAttributeString() method, and then we write the inner text of the element, by calling the WriteString() method. We then make sure to close the first "user" element with a call to the WriteEndElement() method.
相关推荐
Preface Acknowledgements 1 XML for Data 2 XML Protocols 3 Writing XML with Java 4 Converting Flat Files to XML 5 Reading XML 6 SAX 7 The XMLReader Interface 8 SAX ...
With this book, you’ll learn everything you need to know from the basics of reading and writing XML data to using the DOM, from LINQ and SQL Server integration to SOAP and web services. What you’...
Jean Wyrick's rhetorically organized STEPS TO WRITING WELL WITH ADDITIONAL READINGS is known for its student-friendly tone and the clear way it presents the basics of essay writing in an easy-to-...
famous and useful college writing instructions
Writing Excel Macros with VBA
Stanford-Writing in the Sciences-科学写作课件.rar
Describes the lxml package for reading and writing XML files with the Python programming language.
Writing C language with the CCD camera data acquisition source code, applies to
Writing Excel Macros with VBA, 2nd Edition offers a solid introduction to writing VBA macros and programs, and will show you how to get more power at the programming level: focusing on programming ...
Writing Code with the ADO.NET Data Services Client Library 528 Summary 533 Part 5 Building Desktop User Interfaces 534 24 Taking Console Applications to the Limit 536 Introducing the Password...
latex class for thesis writing. put it in the template.
Data is stored in a linked-list tree structure, preserving the XML data hierarchy, and arbitrary element names, attributes, and attribute values are supported with no preset limits, just available ...
Use this subroutine to define any complex, constitutive models for materials that cannot be modeled with the available ABAQUS material models.
WRITING IN THE TECHNICAL FIELDS A Practical Guide Thorsten Ewald(仅供学习交流使用,侵联删)
These include multitouch, bitmap graphics, interfacing with share and search facilities, printing, working with the sensors (GPS and orientation), text, obtaining input from the stylus (including ...
Laurie Kirszner and Stephen Mandell, authors with thirty years of experience teaching college writing, know what works in the classroom and have a knack for picking just the right readings. In ...
Writing class 1.pages
Writing class 2.pages
Along with these general skills, the authors illustrate several applications that are relevant to data scientists, such as reading and writing spreadsheet documents both locally and via Google Docs, ...
斯坦福大学公开课《writing in the science》的课件,网上找了很久,分享给大家。一共8节课。