简体   繁体   English

如何查找和替换部分 CDATA 文本。 C#

[英]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.我正在尝试编写一个程序,该程序可以将一些 PLC 文件从物理控制器转换为仿真器,因此我导出了一个 XML 文件,我不想在其中找到所有出现的“:I”并将它们替换为“_I”,文本我不想改变的是在 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.我在这段代码中添加的所有功能仍然需要工作。

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.如果您需要进一步解释,请告诉我。

读取 Html 代码并找到<div>文本。 C#</div><div id="text_translate"><p> 我正在使用此代码阅读 html 页面:</p><pre> WebClient client = new WebClient(); String htmlCode = client.DownloadString(@"C:\checkemails\check.html"); // Replace all html breaks for line seperators. htmlCode = htmlCode.Replace("&lt;br&gt;", "\r\n"); MessageBox.Show(htmlCode);</pre><p> html 页面位于 C:\ 但可以说我正在加载 yahoo.com.. 或任何页面。</p><p> 我需要找到一个名为谁的 id 的 div</p><pre> &lt;div id ="say"&gt;</pre><p> 我该怎么做?</p></div> - Reading Html Code and finding <div> text. C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Regex 查找并替换 C# 将部分文本编码为 base 64 - Regex find and replace C# Encode a part of text in base 64 C#正则表达式查找并替换重用部分匹配文本 - c# regex to find and replace reusing part of the matched text 如何在C#中查找文本并将其替换为图像 - How to find a text and replace it with an image in C# 如何使用正则表达式C#替换文本的某些部分 - How to replace some part of the text using regex c# 如何在C#中替换文本框中的部分文本 - how do I replace part of text in textbox in c# 查找并替换字符串 c# 的一部分 - Find and replace a part of a string c# 在XML属性ID中查找字符串的一部分,并在C#中替换部分 - Find part of a string in XML attribute id and replace part in C# 读取 Html 代码并找到<div>文本。 C#</div><div id="text_translate"><p> 我正在使用此代码阅读 html 页面:</p><pre> WebClient client = new WebClient(); String htmlCode = client.DownloadString(@"C:\checkemails\check.html"); // Replace all html breaks for line seperators. htmlCode = htmlCode.Replace("&lt;br&gt;", "\r\n"); MessageBox.Show(htmlCode);</pre><p> html 页面位于 C:\ 但可以说我正在加载 yahoo.com.. 或任何页面。</p><p> 我需要找到一个名为谁的 id 的 div</p><pre> &lt;div id ="say"&gt;</pre><p> 我该怎么做?</p></div> - Reading Html Code and finding <div> text. C# 使用 C# 在 Excel 中查找和替换文本 - Find and replace text in Excel using C# c#查找和替换Word文档中的文本 - c# Find and Replace Text in a Word Document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM