Update CDATA in xml

I have an xml file containing CDATA

I need to update CDATA, as in this example.

I change here to "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
source share
3 answers

You will need to extract cdata as a regular string and then adjust it using regular string operations (or regular expression) before reinstalling as cdata. This is the nature of the cdata sections.

+4
source

XmlCDataSection Value. , , , LINQ CData, :

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);
    }
}
+5

# 2.0

CDATA InnerText

xmlDoc.DocumentElement.SelectSingleNode("//elements").FirstChild.Value  = 
    "-div[id|dir|class|align|style], span[class|align|style]";

string xmlPath = @"C:\yourFolder\yourFile.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
xmlDoc.DocumentElement.SelectSingleNode("//elements").FirstChild.Value  = 
    "-div[id|dir|class|align|style], span[class|align|style]";
xmlDoc.Save(xmlPath);
+1
source

Source: https://habr.com/ru/post/1754354/


All Articles