all groups > dotnet xml > march 2006 >
You're in the

dotnet xml

group:

xmlTextWriter



xmlTextWriter Gilgamesh
3/31/2006 2:50:17 PM
dotnet xml: Hello,
How do I use this class to create an XML document without saving it into a
file? I need to store the XML documen into a string so it could be passed as
a parameter to another class.
Gilgamesh

Re: xmlTextWriter Zafar Abbas
3/31/2006 3:52:34 PM
Create an XmlTextWriter through the constructor which takes in a TextWriter.
You could create a StringWriter instance of the abstract TextWriter class
and store the content written by the XmlTextWriter is that string without
ever persisting it into a file.

Thanks,
Zafar

[quoted text, click to view]

Re: xmlTextWriter Gilgamesh
3/31/2006 4:44:16 PM
Thanks, Zafar. I got the XML document written into StringWriter. However, my
ultimate goal is to store XML document in a text field in SQL database. I
tried the StringMethod method called Write, but this method inserts \r\n at
the end of each element. Is there any way to get the XML document without
line return characters?
Thanks,
-G


[quoted text, click to view]

Re: xmlTextWriter Gilgamesh
3/31/2006 5:55:01 PM
Just a correction that the method I used was ToString not Write.
-G


[quoted text, click to view]

Re: xmlTextWriter Martin Honnen
4/1/2006 12:00:00 AM


[quoted text, click to view]

That depends solely on the Formatting settings of the XmlTextWriter e.g.
you can do

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.None;
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteElementString("god", "Xibo");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
Console.WriteLine("XML is:\r\n{0}", stringWriter.ToString());

and the result is alike

XML is:
<gods><god>Kibo</god><god>Xibo</god></gods>


or you can set the writer to indent e.g.

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteElementString("god", "Xibo");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
Console.WriteLine("XML is:\r\n{0}", stringWriter.ToString());

and the output is alike

XML is:
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>


--

Martin Honnen --- MVP XML
Re: xmlTextWriter Gilgamesh
4/3/2006 10:07:06 AM
Thanks, Martin. I appreciate your help.
BTW, how can I change the encoding to utf-8?
-G
[quoted text, click to view]

AddThis Social Bookmark Button