473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XmlTextWriter

Can anybody how do I use XmlTextWriter example?

I want to write such XML file:

<?xml version="1.0" standalone="yes"?>
<Clients>
<Client>
<Name>David</Name>
<Age>27</Age>
</Client>
<Client>
<Name>George</Name>
<Age>13</Age>
</Client>
</Clients>
Nov 16 '05 #1
2 8821


Ghost wrote:
Can anybody how do I use XmlTextWriter example?

I want to write such XML file:

<?xml version="1.0" standalone="yes"?>
<Clients>
<Client>
<Name>David</Name>
<Age>27</Age>
</Client>
<Client>
<Name>George</Name>
<Age>13</Age>
</Client>
</Clients>


Straight-forward (example writes only one <Client> element, it should be
clear how to write further ones):

using System;
using System.Xml;

public class Test2004080701 {

public static void Main (string[] args) {
XmlTextWriter xmlWriter = new
XmlTextWriter("test20040807XmlWriterTest.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement("Clients");
xmlWriter.WriteStartElement("Client");
xmlWriter.WriteStartElement("Name");
xmlWriter.WriteString("David");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Age");
xmlWriter.WriteString("27");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
}

}

Shorter and more elegantly

using System;
using System.Xml;

public class Test2004080701 {

public static void Main (string[] args) {
XmlTextWriter xmlWriter = new
XmlTextWriter("test20040807XmlWriterTest.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement("Clients");
xmlWriter.WriteStartElement("Client");
xmlWriter.WriteElementString("Name", "David");
xmlWriter.WriteElementString("Age", "27");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
}

}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #2
Hi david
There are many ways to use the XmlWriter object . for example this
snippet would write your provided sample xml to file on the root c
XmlTextWriter write = new XmlTextWriter("c:\\ writerXML.xml",
System.Text.Encoding.Unicode);
write.WriteStartDocument();
write.WriteStartElement("Clients");

write.WriteStartElement("client");

write.WriteStartElement("Name");
write.WriteString("David");
write.WriteEndElement();

write.WriteStartElement("Age");
write.WriteString("27");
write.WriteEndElement();

write.WriteEndElement();
write.WriteStartElement("client");

write.WriteStartElement("Name");
write.WriteString("George");
write.WriteEndElement();

write.WriteStartElement("Age");
write.WriteString("13");
write.WriteEndElement();

write.WriteEndElement();
write.WriteEndElement();

write.Flush();

Another way to write the same XML however would be
write.WriteRaw("<?xml version="1.0" standalone="yes"?>
<Clients>
<Client>
<Name>David</Name>
<Age>27</Age>
</Client>
<Client>
<Name>George</Name>
<Age>13</Age>
</Client>
</Clients>");

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
1
by: Koray Atsan | last post by:
Hi All I have a simple question as i am a newbie in xml. I am using XmlTextWriter to write some values to an xml file . I use XmlTextWriter textwriter = new XmlTextWriter ("path",null); to create...
3
by: Magnus | last post by:
can anyone help me on how to create and manipulate a xmttextwriter without having to craete a physical file. I have an application that should return data in xml. But I do not want to create a...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
3
by: K Rege | last post by:
Hi there, I tried to implement a little XML-EchoServer using Sockets and XmlTextWriter/Reader. However the server hangs while reading the input from the client (wr.Flush() seams not to work). I...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
2
by: Steve | last post by:
I'm an XML newb. I'm serializing a class and when I inspect the xml file, all the data is on one line rather than being nested and indented Is that normal? <code> StreamWriter sw = new...
4
by: Einar Høst | last post by:
Hi, I'm having weird problems using StringWriter and XmlTextWriter. My code looks like this: StringWriter sw = new StringWriter(CultureInfo.InvariantInfo); XmlTextWriter xtw = new...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. When I instantiate the XmlTextWriter to a file......
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.