DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: How to put a string value inside CDATA block?

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    How to put a string value inside CDATA block?

    I want to serialize a class to a string (who is a property of another class). I do it like this:

    (schema is the XML I want to put in the string.)

    Dim ser As New XmlSerializer(GetType(schemaobject))
    Dim sw As New IO.StringWriter()
    ser.Serialize(sw, schema)

    sw.ToString will give the xml, and I can write:

    Dim objReader As System.IO.StreamWriter
    objReader = New StreamWriter("C:\xmlfile.txt")
    objReader.Write(sw.ToString)
    objReader.Close()

    But I donít want to write the xml to file I want it stored in a string variable, and I need to put the whole xml inside a CDATA block, how can I do that? I will put sw.ToString (that is my xml) inside a CDATA block and then store my xml inside the CDATA in a string.
    How can I do that in VB.NET?

    Thanks in advance.
    Lise

  2. #2
    Join Date
    May 2009
    Posts
    60

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    I looked at the links but I still not manage to get what I want.
    I have auto generated a class from an xsd schema. I use auto generated class to generate my ml document. I want my xml document as a string because I want to put it in a variable not write it to a file. My string variable will be a property of another class, and the object of this class will be sent to a webservice.

    My xml document must look like this:
    Code:
    <![CDATA[
    <?xml version="1.0"?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    ]]>
    I do not want:
    Code:
    <?xml version="1.0"?>
    <note>
      <to><![CDATA[Tove]]></to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    How can I do this?

    Thanks!

  4. #4
    Join Date
    May 2009
    Posts
    60
    http://msdn.microsoft.com/en-us/library/bb384832.aspx

    http://msdn.microsoft.com/en-us/library/bb384869.aspx


    Code:
    Dim str As String = _
        "<![CDATA[" & _
        "<?xml version= '1.0'?>" & _
        "<!-- comment at the root level -->" & _
        "<Root>" & _
        "  <Child>Content</Child>" & _
        "</Root>"" & _
        "]]>"
    
    Dim doc As XDocument = XDocument.Parse(str)
    Console.WriteLine(doc)
    http://msdn.microsoft.com/en-us/library/bb345532.aspx
    Last edited by xml-looser; 04-05-2011 at 10:01 AM.

  5. #5
    Join Date
    Apr 2011
    Posts
    3
    Thanks for your reply!

    My problem is that I canít build my xml string (document) as you suggest (I thinkÖ)

    I have to do it like this (The xml in my last reply was only an example, my xml string is a lot bigger):

    Dim ser As New XmlSerializer(GetType(schemaobject))
    Dim sw As New IO.StringWriter()
    ser.Serialize(sw, schema)

    Schema is a auto generated class from a xsd document. I serialize the object of this class to a string called sw (I can also serialize it to a document but I want the xml as a string). Then I have my xml string and I want to put the content of this string inside a CDATA section.

    How can I do that?

    Thanks.

  6. #6
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49

    C# Solution

    Apologies for the C# but something along the lines of this should work:

    Code:
        System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
    
        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(GetType(schemaobject));
            xmlSerializer.Serialize(memoryStream, schema);
    
            // Gets you your XML...
            xmlDoc.Load(memoryStream);
        }
    
        // Wrap in CData Section
        XmlCDataSection cData = xmlDoc.CreateCDataSection(xmlDoc.OuterXml);
    
        // Should give you what you need....
        string cDataString = cData.OuterXml;

  7. #7
    Join Date
    Apr 2011
    Posts
    3
    Thanks reedy837, it was exactly what I needed

Similar Threads

  1. Accessing Icons in the Image Folder
    By rrjii2000 in forum .NET
    Replies: 35
    Last Post: 09-08-2010, 01:02 PM
  2. Replies: 1
    Last Post: 07-08-2002, 12:19 PM
  3. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 04:53 AM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  5. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center