By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

XmlTextWriter

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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.