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 ;"
Posted

1 solution

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

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:
C#
XElement xeChildValue = new XElement(children[i]);
xeChildValue.Add(new XCData(values[i]));
xeChild.Add(xeChildValue);

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

With a bit of LINQ, you can even replace your inner loop:
C#
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))
    ));
}
   
v2

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