CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2002
    Posts
    42

    Question how to create a xml and write to a string variable?

    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

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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
    Last edited by pareshgh; February 21st, 2003 at 02:27 PM.

  3. #3
    Join Date
    Oct 2002
    Posts
    42
    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

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  5. #5
    Join Date
    Oct 2002
    Posts
    42
    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

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  7. #7
    Join Date
    Oct 2002
    Posts
    42
    sounds a good option. will try it out. thanks a lot

  8. #8
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    let me know that it worked for u .. so that later in my app i can also use the same..

  9. #9
    Join Date
    Oct 2002
    Posts
    42
    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();

  10. #10
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  11. #11
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Another approach, if you already have XML Document loaded:

    Code:
    /////////////////
    // 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

  12. #12
    Join Date
    Oct 2002
    Posts
    42
    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()?

  13. #13
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    i don't think so. dispose takes place when local var. are candidates to garbage..

    Paresh

  14. #14
    Join Date
    Mar 2003
    Location
    Frankfurt
    Posts
    1

    string xmlString = xmlDom.ToString();

    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...

  15. #15
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Yes, sure...

    It was a mistake made by inattention... :-(

    martin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured