Search code examples
c#xmlreplacecdata

How to find and replace part of CDATA text. c#


I am trying to write a program which can convert some PLC files from physical controllers to emulators therefore I have exported an XML file where i wan't to find all occurrence of ":I" and replace them with "_I", the text i wan't to change is within the CDATA. Please check the examble

    <Routines>
      <Routine Name="Aggregate_calls" Type="ST">
        <STContent>
          <Line Number="0"><![CDATA[/* Aggregate_Calls: Processing of Type Circuits]]></Line>
          ...(A lot of Lines/data here.)
          <Line Number="312"><![CDATA[_1000017_Data_In.ConnectionFaulted := VFD_1000017:I1.ConnectionFaulted;]]></Line>

The code i already have written is this:

 static void Main(string[] args)
    {
        Console.WriteLine(@"Insert full path of the XML files. ex. C:\Users\ECR\Desktop");

        var folder = Console.ReadLine()+@"\";

        string[] files = System.IO.Directory.GetFiles(folder, "*.L5X");

        foreach (var CLX in files)
        {                
            var xDoc = XDocument.Load(CLX); //XDocument.Load(filename)

            foreach (var CPU in xDoc.Descendants().Elements("Controller"))
            {
                CPU.Attribute("ProcessorType").Value = "Emulate 5570";

                var attribute = new XAttribute("TimeSlice", 20);
                CPU.Add(attribute);
            }


            //  I WAN'T TO INSERT THE CODE HERE.


            var moduleList = xDoc.Descendants("Modules").Elements().ToList();

            foreach (var module in moduleList)
            {
                module.Remove();
            }

            var xDocEmu = XDocument.Load("EmuBaseObject.L5X"); //XDocument.Load(filename)

            var moduleListEmu = xDocEmu.Descendants("Modules").Elements().ToList();


            xDoc.Descendants("Modules").Single().Add(moduleListEmu[0]);

            var filename = Path.GetFileName(CLX);

            var date = DateTime.Now.ToString("yyyy.MM.dd - HH.mm");

            var outputfolder = folder + @"\Emulate Files\" + date;

            System.IO.Directory.CreateDirectory(outputfolder);

            xDoc.Save(outputfolder + @"\" + filename);

            Process.Start(outputfolder);
        }
    }

All the functions i have added in this code still need to work.


Solution

  • Depending on the path to nodes you want to change, this does the trick for me.

     var cdata = xDoc.Element("Routines").Descendants("Routine").Descendants("STContent").Descendants().Where(_ => _.FirstNode.NodeType == System.Xml.XmlNodeType.CDATA).ToList();
    
     cdata.ForEach(_ => _.Value = _.Value.Replace(":I", "_I"));
    

    Let me know if you need further explanation.