Click here to Skip to main content
15,308,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello every one.
in my text to xml conversion when i insert following line of code to get values within cdata
xeChildValue.Value = String.Format("<![CDATA[{0}", values[i]);

the tags for cdata < and > are not printed in xml file instead their values are displayed.
But i want open and close tags to be displayed in my xml doc not their values. so wat must i do?
note: the value that come for < is " & l t ;"
the value that come for > is " & g t ;"

1 solution

Assuming you're using System.Xml and the XmlDocument, you need to add a child node:
XmlCDataSection cdata = xeChildValue.OwnerDocument.CreateCDataSection(values[i]);

XmlDocument.CreateCDataSection Method[^]

EDIT: Based on your previous copy of this question[^], you're using System.Xml.Linq and XDocument. That makes things slightly easier:
XElement xeChildValue = new XElement(children[i]);
xeChildValue.Add(new XCData(values[i]));

Or, more concisely:
xeChild.Add(new XElement(children[i], new XCData(values[i])));

With a bit of LINQ, you can even replace your inner loop:
foreach (Capture cap in m.Groups["values"].Captures)
    string[] values = cap.Value.Split(new string[] { "@*@" }, StringSplitOptions.None);
    if (children.Length != values.Length)
        throw new InvalidOperationException("The number of children and values mismatch.");
    xeParent.Add(new XElement("data", chilren.Zip(values, 
        (name, value) => new XElement(name, new XCData(value))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900