472,325 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 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 8716


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...
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...
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...
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...
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...
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...
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...
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...
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: ...
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. ...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...

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.