none
Preserve Format of Text In CDATA

    Question

  • Hello everyone,

    I'm currently working on a utility application to display text from an XML file. The certain tag that holds all the info has a CDATA tag in it with text that has been formatted with whitespace and newlines. I need to display that text from the CDATA tag in either a text box or a rich textbox, whichever works.

    Currently I can pull all the text in, but when it display, currently in a rich text box, it loses the format it was in and all whitespace and new lines are removed.

    Anyone know a way to do that without me having to reformat the text myself to display it?

    Friday, October 28, 2011 5:15 PM

Answers

  • > I need to display that text from the CDATA tag in either a text box or a rich textbox, whichever works.

     
    below is an example of binding textbox to cdata

    using System.Data;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                var ds = new DataSet();
                // load from string for test
                ds.ReadXml(new StringReader(@"
                    <root>
                        <data id='1'><![CDATA[v1]]></data>
                        <data id='2'><![CDATA[v2]]></data>
                    </root>"));
    
                var tb = new TextBox();
                tb.Parent = this;
                tb.Dock = DockStyle.Top;
                // bind TextBox to data/cdata
                tb.DataBindings.Add("Text", ds.Tables["data"], "data_Text");
    
                // for test
                var dg = new DataGrid();
                dg.Parent = this;
                dg.Dock = DockStyle.Fill;
                dg.DataSource = ds.Tables["data"];        
            }
        }
    }
    

    Friday, October 28, 2011 7:26 PM

All replies

  • How are you accessing Cdata Section From XML ? From Xpath or through XMLDocument ? This works fine for me,

    <?xml version="1.0"?>
    <para>
      <![CDATA[Lingaraj Mishra 
     Hello
       Hi
     Stupid ]]>
    </para>
    

     

    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(@"d:\XMLFile1.xml");
    richTextBox1.Text = xDoc.ChildNodes[1].InnerText;
    

     


    Lingaraj Mishra
    Friday, October 28, 2011 7:07 PM
  • > I need to display that text from the CDATA tag in either a text box or a rich textbox, whichever works.

     
    below is an example of binding textbox to cdata

    using System.Data;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                var ds = new DataSet();
                // load from string for test
                ds.ReadXml(new StringReader(@"
                    <root>
                        <data id='1'><![CDATA[v1]]></data>
                        <data id='2'><![CDATA[v2]]></data>
                    </root>"));
    
                var tb = new TextBox();
                tb.Parent = this;
                tb.Dock = DockStyle.Top;
                // bind TextBox to data/cdata
                tb.DataBindings.Add("Text", ds.Tables["data"], "data_Text");
    
                // for test
                var dg = new DataGrid();
                dg.Parent = this;
                dg.Dock = DockStyle.Fill;
                dg.DataSource = ds.Tables["data"];        
            }
        }
    }
    

    Friday, October 28, 2011 7:26 PM
  • Hi forsberg,

    It doesn’t lose the format when I try to display CDATA tag data in a rich text box. Could you please provide more details, say:

    The code loads the xml file.

    The code read the CDATA tag.

    A simple XML file.

    if you try to display in TextBex, set Multiline to true.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 01, 2011 9:51 AM