Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating CDATA in xml

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

like image 294
SAK Avatar asked May 19 '26 07:05

SAK


1 Answers

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);
    }
}
like image 57
Jon Skeet Avatar answered May 20 '26 19:05

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!