views:

55

answers:

2

hello folks.,

i have xml file which contains CDATA

i need to update the CDATA as like in this example.

i am modifying "span" here

<elements>
    <![CDATA[-div[id|dir|class|align|style],-span[class|align]]]>
  </elements>

should be updated as

<elements>
    <![CDATA[-div[id|dir|class|align|style],-span[class|align|style]]]>
  </elements>

i am using framework 2.0.. how to do this using xmldocument.

thank you

+3  A: 

You will need to extract the cdata as a regular string, then adjust it using normal string operations (or a regex) before re-inserting as cdata. That is the nature of cdata sections.

Brent Arias
That is the nature of text nodes. It doesn't matter if they are expressed using CDATA or not.
David Dorward
+3  A: 

Just fetch the XmlCDataSection and change the Value property. Here's an example which admittedly uses LINQ to find the CData section, but the principle of changing it would be the same:

using System;
using System.Linq;
using System.Xml;

class Test
{
    static void Main(string[] args)
    {
        string xml = 
@"<elements>
    <![CDATA[-div[id|dir|class|align|style],-span[class|align]]]>
</elements>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XmlCDataSection cdata = doc.DocumentElement
                                   .ChildNodes
                                   .OfType<XmlCDataSection>()
                                   .First();
        cdata.Value = "-div[id|dir|class|align|style],-span[class|align|style]";
        doc.Save(Console.Out);
    }
}
Jon Skeet
@sam: Well it's up to you to implement the logic for working out what data to set. The point of my example was to demonstrate how to change the value of a CData section.
Jon Skeet