Reading and Writing XML in C#In this article, you will see how to read and write XML documents in Microsoft . NET using C# language. First, I will discuss XML . NET Framework Library namespace and classes. Then, you will see how to read and write XML documents. In the end of this article, I will show you how to take advantage of ADO. NET and XML . NET model to read and write XML documents from relational databases and vice versa. Introduction to Microsoft .
NET XML Namespaces and Classes. Before start working with XML document in . NET Framework, It is important to know about .
- Serialization How to: Write Object Data to an XML File. How to: Write Object Data to an XML File (C#) Show: Inherited Protected Print Export (0).
- A chapter on Writing XML with the XmlDocument class in the complete Microsoft C#. to go into an existing XML document and. XML file becomes a.
- Reading and Writing XML in C#. which creates an XML document and write its contents to the XML. // write dataset contents to an xml file by calling WriteXml.
- How to create xml document programatically in c#. Rate this: Please. how to save xml file in current project folder.how to download that file.
- Vengono descritte alcune procedure di esempio per leggere dati XML da un file. (Document XML). informazioni sulle classi XML di.NET Framework e C#.
NET namespace and classes provided by . NET Runtime Library.
NET provides five namespace - System. Xml, System. Xml.
How can I create a simple xml file and store it in my system? C# create simple xml file [closed]. creating new xml file by C#. 0. Fragment that calls XmlWriter.Create: C#. Token StartElement in state EndRoot Element would result in an invalid XML document. Contents of output file: XML.
. I will show you how to use XmlTextWriter class to create an XML document and write. Write XML in C#. If you don't want to write data in an XML file.
Schema, System. Xml. Serialization, System. Xml. XPath, and System. Xml. Xsl to support XML classes. The System. Xml namespace contains major XML classes. This namespace contains many classes to read and write XML documents.
In this article, we are going to concentrate on reader and write class. These reader and writer classes are used to read and write XMl documents. These classes are - Xml. Reader, Xml. Text. Reader, Xml. Validating. Reader, Xml. Node.
Reader, Xml. Writer, and Xml. Text. Writer. As you can see there are four reader and two writer classes. The Xml. Reader class is an abstract bases classes and contains methods and properties to read a document. The Read method reads a node in the stream.
Besides reading functionality, this class also contains methods to navigate through a document nodes. Some of these methods are Move. To. Attribute, Move. To. First. Attribute, Move.
To. Content, Move. To. First. Content, Move. To. Element and Move.
To. Next. Attribute. Read. String, Read.
Inner. Xml, Read. Outer. Xml, and Read. Start. Element are more read methods.
This class also has a method Skip to skip current node and move to next one. We'll see these methods in our sample example. The Xml. Text. Reader, Xml. Node. Reader and Xml. Validating. Reader classes are derived from Xml. Reader class. As their name explains, they are used to read text, node, and schemas. The Xml. Write class contains functionality to write data to XML documents.
This class provides many write method to write XML document items. This class is base class for Xml. Text. Writer class, which we'll be using in our sample example. The Xml.
Node class plays an important role. Although, this class represents a single node of XML but that could be the root node of an XML document and could represent the entire file. This class is an abstract base class for many useful classes for inserting, removing, and replacing nodes, navigating through the document. It also contains properties to get a parent or child, name, last child, node type and more. Three major classes derived from Xml. Node are Xml. Document, Xml.
Data. Document and Xml. Document. Fragment.
Xml. Document class represents an XML document and provides methods and properties to load and save a document. It also provides functionality to add XML items such as attributes, comments, spaces, elements, and new nodes. The Load and Load. Xml methods can be used to load XML documents and Save method to save a document respectively. Xml. Document. Fragment class represents a document fragment, which can be used to add to a document. The Xml. Data. Document class provides methods and properties to work with ADO. NET data set objects.
In spite of above discussed classes, System. Xml namespace contains more classes. Few of them are Xml. Convert, Xml. Linked.
Node, and Xml. Node. List. Next namespace in Xml series is System. Xml. Schema. It classes to work with XML schemas such Xml. Schema, Xml. Schema. All, Xml. Schema.
XPath, Xml. Schema. Type. The System. Xml. Serialization namespace contains classes that are used to serialize objects into XML format documents or streams. The System. Xml. XPath Namespce contains XPath related classes to use XPath specifications. This namespace has following classes - XPath. Document, XPath. Exression, XPath. Navigator, and XPath.
Node. Iterator. With the help of Xpath. Document, Xpath. Navigator provides a fast navigation though XML documents. This class contains many Move methods to move through a document. The System. Xml. Xsl namespace contains classes to work with XSL/T transformations.
Reading XML Documents. In my sample application, I'm using books. Xml. Text. Reader. This file comes with VS. NET samples. You can search this on your machine and change the path of the file in the following line: Xml.
Text. Reader text. Reader = new Xml.
Text. Reader("C: \\books. Or you can use any XML file. The Xml. Text. Reader, Xml. Node. Reader and Xml. Validating. Reader classes are derived from Xml.
Reader class. Besides Xml. Reader methods and properties, these classes also contain members to read text, node, and schemas respectively. I am using Xml. Text.
Reader class to read an XML file. You read a file by passing file name as a parameter in constructor. Xml. Text. Reader text. Reader = new Xml. Text. Reader("C: \\books. After creating an instance of Xml. Text. Reader, you call Read method to start reading the document.
After read method is called, you can read all information and data stored in a document. Xml. Reader class has properties such as Name, Base. URI, Depth, Line.
Number an so on. List 1 reads a document and displays a node information using these properties. About Sample Example 1. In this sample example, I read an XML file using Xml. Text. Reader and call Read method to read its node one by one until end of file and display the contents to the console output. Sample Example 1. System; using System. Xml; namespace Read. Xml. 1{ class.
Class. 1 { staticvoid Main(string[] args) { // Create an isntance of Xml. Text. Reader and call Read method to read the file Xml. Text. Reader text. Reader = new. Xml.
Text. Reader("C: \\books. Reader. Read(); // If the node has value while (text. Reader. Read()) { // Move to fist element text. Reader. Move. To. Element(); Console. Write. Line("Xml.
Text. Reader Properties Test"); Console. Write. Line("==================="); // Read this element's properties and display them on console Console. Write. Line("Name: " + text. Reader. Name); Console. Write. Line("Base URI: " + text.
Reader. Base. URI); Console. Write. Line("Local Name: " + text. Reader. Local. Name); Console. Write. Line("Attribute Count: " + text. Reader. Attribute.
Count. To. String()); Console. Write. Line("Depth: " + text. Reader. Depth. To. String()); Console. Write. Line("Line Number: " + text. Reader. Line. Number. To. String()); Console.
Write. Line("Node Type: " + text. Reader. Node. Type. To. String()); Console. Write. Line("Attribute Count: " + text. Reader. Value. To. String()); } } }}The Node.
Type property of Xml. Text. Reader is important when you want to know the content type of a document. The Xml. Node. Type enumeration has a member for each type of XML item such as Attribute, CDATA, Element, Comment, Document, Document. Type, Entity, Process. Instruction, White. Space and so on. List 2 code sample reads an XML document, finds a node type and writes information at the end with how many node types a document has. Highly Recommended.
I have published a free book on XML programming using C#. Get your free copy here. Free e- book: Programming XML with C#Learn more XML Programming. Still hungry for more XML programming with C# and .
NET? Here is a dedicated section with hundreds of articles and code samples on XML programming using C# and . NET. XML Programming in C#About Sample Example 2. In this sample example, I read an XML file using Xml. Text. Reader and call Read method to read its node one by one until end of the file. After reading a node, I check its Node. Type property to find the node and write node contents to the console and keep track of number of particular type of nodes.
In the end, I display total number of different types of nodes in the document. Sample Example 2. System; using System. Xml; namespace Reading. XML2{ class. Class. Main(string[] args) { int ws = 0; int pi = 0; int dc = 0; int cc = 0; int ac = 0; int et = 0; int el = 0; int xd = 0; // Read a document Xml.
Text. Reader text. Reader = new. Xml. Text. Reader("C: \\books. Read until end of file while (text. Reader. Read()) { Xml. Node. Type n. Type = text. Reader. Node. Type; // If node type us a declaration if (n.
Type == Xml. Node. Type. Xml. Declaration) { Console. Write. Line("Declaration: " + text. Reader. Name. To. String()); xd = xd + 1; } // if node type is a comment if (n.
Type == Xml. Node. Type. Comment) { Console. Write. Line("Comment: " + text. Reader. Name. To. String()); cc = cc + 1; } // if node type us an attribute if (n.
Type == Xml. Node. Type. Attribute) { Console. Write. Line("Attribute: " + text. Reader. Name. To. String()); ac = ac + 1; } // if node type is an element if (n. Type == Xml. Node.
Type. Element) { Console. Write. Line("Element: " + text. Reader. Name. To. String()); el = el + 1; } // if node type is an entity\ if (n. Type == Xml. Node. Type. Entity) { Console. Write. Line("Entity: " + text.
Reader. Name. To.
Write XML in C#The Xml. Writer and Xml. Text. Writer classes are defined in the System.
XML namespace. The Xml. Text. Writer class is derived from Xml.
Writer class, which represents a writer that provides fast non- cached forward- only way of generating XML documents based on the W3. C Extensible Markup Language (XML) 1. In this article, I will show you how to use Xml.
Text. Writer class to create an XML document and write data to the document. Adding namespace Reference. Since Xml classes are defined in the System. XML namespace, so first thing you need to do is to Add the System. XML reference to the project.
System. Xml; Creating an XML Document. The constructor of the Xml. Text. Writer class creates an XML file if file doesn't exist.
In this sample, I create a new XML file called xmltest. C\temp directory.
Xml. Text. Writer writer = new Xml. Text. Writer("C: \\temp\\xmltest. NOTE: If you don't want to write data in an XML file and want to display XML contents on the Console, pass Console.
Out as a parameter of the constructor. Xml. Text. Writer writer = new Xml. Text. Writer(Console. Out); Adding Data to the Document. The Write. Start.
Document method starts a new document. The Write. Start. Element and the Write. End. Element pair is used to add a new element to the document. The Write. String writes a string to the document.
Write. Start. Document(); writer. Write. Comment("Commentss: Xml.
Writer Test Program"); writer. Write. Processing. Instruction("Instruction","Person Record"); writer. Write. Start. Element("p", "person", "urn: person"); writer. Write. Start. Element("Last. Name",""); writer. Write. String("Chand"); writer.
Write. End. Element(); writer. Write. Element. Int. Write. End. Document(); Souce Code: Attachment createxml.
KBnamespace Write. To. XML { using System; using System. Xml; ///< summary> /// Summary description for Class. Class. 1 { public Class. Main(string[] args) { try{ // Creates an XML file is not exist Xml. Text. Writer writer = new Xml. Text. Writer("C: \\temp\\xmltest.
Starts a new document writer. Write. Start. Document(); //Write comments writer. Write. Comment("Commentss: Xml. Writer Test Program"); writer. Write. Processing. Instruction("Instruction","Person Record"); // Add elements to the file writer.
Write. Start. Element("p", "person", "urn: person"); writer. Write. Start. Element("Last. Name",""); writer. Write. String("Chand"); writer. Write. End. Element(); writer. Write. Start. Element("First.
Name",""); writer. Write. String("Mahesh"); writer.
Write. End. Element(); writer. Write. Element. Int. Ends the document writer. Write. End. Document(); } catch (Exception e) { Console.
Write. Line ("Exception: {0}", e. To. String()); } return 0; } }}.