469,602 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

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 8558


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jain, Pranay Kumar | last post: by
1 post views Thread by Koray Atsan | last post: by
3 posts views Thread by Magnus | last post: by
3 posts views Thread by K Rege | last post: by
1 post views Thread by Riko Eksteen | last post: by
4 posts views Thread by Einar HÝst | last post: by
4 posts views Thread by quest | last post: by
3 posts views Thread by GaryDean | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.