Click to See Complete Forum and Search --> : how to create a xml and write to a string variable?
benchris
February 21st, 2003, 01:03 PM
Hi,
I am trying to create a customized xml doc and save it to a string, so I can send it out by socket.
However, I did not see sample in xmlwriter to do it (I don't want to create a file at the local). Can anyone show me what would be the best way to do it?
Thanks a lot
Chris
pareshgh
February 21st, 2003, 01:09 PM
you need to use LoadXML
like this
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
//Save the document to a file.
doc.Save("data.xml");
}
}
-Paresh
benchris
February 21st, 2003, 01:25 PM
thanks. But I really want to do is to use xmldocument (or xmlwriter or whatever xml creatinging tool) to create and manipulate a xml format string, as adding a node or removing a node. Afterwards, I would like to save the whole xml as a local string variable, then send it out by socket. I do not want to save it to any local file. Is there a way to do it?
Thanks
Chris
pareshgh
February 21st, 2003, 01:29 PM
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
you can use the string anyway.. here ..
if you have a XML creater then you will need to write a wrapper which interacts with that creater and ur program. after doing that. you can use the string over the socket. from string also u can load XML.. and/or from saved one.
Paresh
benchris
February 21st, 2003, 01:37 PM
Thanks pareshgh. BUt this is different. You basically almost write the whole xml by your self. What I really need is to use SAX or DOM interface to create xml from the scrach, add and remove nodes maybe. As
XmlTextWriter tw=new XmlTextWriter(...)
tw.WriteStartDocument();
tw.WriteStartElement("book");
tw.WriteAttributeString("price","10");
...
at the end, I would like to import this created xml stream to a string, but not to a file as XmlTexWriter commonly did.
I noticed that XmlTextWriter may take stream argument but I jsut did not figure out how to import it to a string variable, but not to a file. And I do not care to use xmldocument or something else, instead of xmlwriter, to do this job. Any suggestions?
Thanks
Chris
pareshgh
February 21st, 2003, 01:55 PM
i don't know what constructor u r using with XmlTextWriter.. but
with
public XmlTextWriter(
Stream w,
Encoding encoding
);
stream which can be temporary in memory and then u can pass stream to sockets also...
Paresh
benchris
February 21st, 2003, 03:10 PM
sounds a good option. will try it out. thanks a lot
pareshgh
February 21st, 2003, 03:16 PM
let me know that it worked for u .. so that later in my app i can also use the same..
:D ;)
benchris
February 25th, 2003, 03:40 PM
Would this be the best way to do this kind of work? Should any lines be removed or added (especially at the clean up section)?
StringWriter sw=new StringWriter();
XmlTextWriter tw=new XmlTextWriter(sw);
//Start creating elements and attributes
tw.WriteStartElement("book");
tw.WriteAttributeString("genre","Mystery");
tw.WriteAttributeString("publicationdate","2001");
tw.WriteAttributeString("ISBN","123456789");
tw.WriteElementString("title","Case of the Missing Cookie");
tw.WriteStartElement("author");
tw.WriteElementString("name","Cookie Monster");
tw.WriteEndElement();
tw.WriteElementString("price","9.99");
tw.WriteEndElement();
tw.WriteEndDocument();
//clean up
tw.Flush();
tw.Close();
sw.Close();
String s=sw.ToString();
pareshgh
February 25th, 2003, 03:55 PM
yes I agree here on this code with you..
since you need to send string. so you are using stringbuilder which is used in xmltextwriter and FYI you convert in string. make sure the finnal string is correct.
Paresh
MartinL
February 26th, 2003, 02:08 AM
Another approach, if you already have XML Document loaded:
/////////////////
// usings
using System;
using System.Text;
using System.Xml;
using System.IO;
/////////////////
// Code
XmlDocument xmlDom = new XmlDocument();
// Populate xmlDom with XML data you need...
// ...
// Get the string representation of xmlDom
StringWriter xmlWriter = new StringWriter();
xmlDom.Save(xmlWriter);
string xmlString = xmlDom.ToString();
Martin
benchris
February 26th, 2003, 08:19 AM
Great to generalize all approaches in one thread. Thanks.
BTW, do you think I need to close StringWriter, and close and FLUSH xmltextwriter if i only need to call tostring()?
pareshgh
February 26th, 2003, 12:47 PM
i don't think so. dispose takes place when local var. are candidates to garbage..
Paresh
tolgasevim
March 21st, 2003, 04:05 AM
Hi all,
I just wanted to mention that the toString method is going to produce this output:
System.Xml.XmlDocument
instead of the xml document itself.
Instead you should use toString of StringWriter after you close it.
This way it works fine but that way it writes the XML declaration with version attribute set to "1.0" which I don't want since I want to store the string in database.
Here's my question:
Is it possible use the XMLTextWriter without having it create
<?xml version="1.0" encoding="utf-16"?>
element?
Thanks in advance...
MartinL
March 21st, 2003, 05:59 AM
Yes, sure...
It was a mistake made by inattention... :-(
martin
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.