none
C# Create duplicate of an xml node inside of CData (which itself in an xml file). RRS feed

  • Question

  • <?xml version='1.0' encoding='UTF-8' ?> <root> <input> <![CDATA[ <ServerLicence>abc</ServerLicence> <inputData> <CreationPreferences></CreationPreferences> <SelectObj1> (?xml version="1.0" encoding="UTF-8"?)(FILTER CASESENSITIVE="TRUE" DEPTH="-1" RESULTSPERPAGE="-1" COMMAND="QUERY" ) (SELECT) (/SELECT) (WHERE) (CONDITION) (EXPRESSION) (VALUE) Replace_Placeholder (VALUE) (/EXPRESSION) (/CONDITION) (/WHERE) </SelectObj1> </inputData> ]]> </input> </root>

    I have an xml file as seen, and it has some CData content, which is also xml format. I have a requirement to replace text inside Cdata- "Replace_Placeholder" in the code according to some arguments it recieves. So when there is more than 1 argument , the xml should look like:

    <?xml version='1.0' encoding='UTF-8' ?>
    <root>
      <input>
    		<![CDATA[ 
    			<ServerLicence>abc</ServerLicence> 
    			<inputData>
    				 <CreationPreferences></CreationPreferences>
    				 
    				 <SelectObj1>
    				 (?xml version="1.0" encoding="UTF-8"?)(FILTER 
    				 CASESENSITIVE="TRUE" DEPTH="-1" RESULTSPERPAGE="-1" 
    				 COMMAND="QUERY" )
    				 (SELECT)
    				 (/SELECT)
    				 (WHERE)
    				   (CONDITION)
    					(EXPRESSION)
    					 (VALUE)
    					  Replace_Placeholder
    					 (VALUE)
    					(/EXPRESSION)
    				   (/CONDITION)
    				 (/WHERE)
    				</SelectObj1> 
    				
    				<SelectObj1>
    				 (?xml version="1.0" encoding="UTF-8"?)(FILTER 
    				 CASESENSITIVE="TRUE" DEPTH="-1" RESULTSPERPAGE="-1" 
    				 COMMAND="QUERY" )
    				 (SELECT)
    				 (/SELECT)
    				 (WHERE)
    				   (CONDITION)
    					(EXPRESSION)
    					 (VALUE)
    					  Replace_Placeholder
    					 (VALUE)
    					(/EXPRESSION)
    				   (/CONDITION)
    				 (/WHERE)
    				</SelectObj1> 
    			</inputData>
    	]]>
      </input>
    </root>

    It should create one more node of type "SelectObj1" and replace text in Replace_Placeholder with the value from the argument. I am stuck as to how to create the same node and replace and finally save the xml! I hope my problem is clear!

    Thursday, February 14, 2019 12:52 PM

All replies

  • Hello, 

    >CData content, which is also xml format

    CDATA content is not an XML document. It is an unstructured text data, regardless to how it looks from outside.

    This is a CDATA purpose - store something, what can be anything - xml-with-formatting-error - for example.

    Important is that xml-document parser would NOT parse a content of CDATA.

    Get whole content of CDATA you can by 

    string toText = xmlDoc.OuterXml.Substring(xmlDoc.OuterXml.IndexOf("<![CDATA[") + "<![CDATA[".Length);
    or similar construction.


    Sincerely, Highly skilled coding monkey.

    Thursday, February 14, 2019 1:12 PM
  • I meant the content inside CDATA has nodes, of which i am concerned about and would need to duplicate one of the nodes inside the CDATA. With the above code, i get the content of CDATA as text but how can i fulfil my purpose (my expected xml is also given with the question).
    Thursday, February 14, 2019 1:26 PM
  • As Andrey wrote, it has not.

    There are no nodes inside your CDATA, cause the content is text and e.g. (SELECT)(/SELECT) is not XML, thus not a node.

    The question is: Why that CDATA at all? What's wrong with using plain XML here instead of it?

    Thursday, February 14, 2019 1:43 PM
  • This is an input file for an application so need to keep the format as it is by just duplicating the tag (as it appears it is not a node). Basically i want to replicate some content inside the CDATA.
    Thursday, February 14, 2019 1:46 PM
  • Well, just read the CDATA from your XML and do a string replace for your placeholder.

    Thursday, February 14, 2019 2:00 PM
  • Yes i have done the replace part and it works perfectly, but currently my problem is on how to create the copy of content - SelectObj1 inside CData.
    Thursday, February 14, 2019 2:05 PM
  • >currently my problem is on how to create the copy of content

    Are you asking - how to clone a string content? 

    As soon as you get a string out of CDATA - it's up to you how to process this text. Simply form new string with-all-what you need and replace content of CDATA.

    Again - get string out and put another string in. What will be a content of the string - it's up to you. You can parse it by:
    - simply getting by string.Substring(indexOf(markerstring))
    - regular expression
    - loading xml-fragment to different document
    what ever you known better,



    Sincerely, Highly skilled coding monkey.

    Thursday, February 14, 2019 2:46 PM
  • Yes got it have already started to working on it! Great hints :)
    Thursday, February 14, 2019 2:47 PM
  • Hi sina9386

    Thank you for posting here.

    According to your description, you want to Create a duplicate of an XML node inside of CData.

    You could try the following code to get it.

      static void Main(string[] args)
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(@"C: \test1.xml");
                XmlNodeList nodeList = xmlDocument.GetElementsByTagName("root");
                nodeList = xmlDocument.GetElementsByTagName("input");
                foreach (XmlNode  node in nodeList[0].ChildNodes)
                {
                    if (node is XmlCDataSection)
                    {
                        Console.WriteLine("CDATA is '{0}'", node.InnerText);
                        string m = node.InnerText;
                        int a = m.IndexOf("</SelectObj1>") - m.IndexOf("<SelectObj1>")+ ("</SelectObj1>").Length+7;
                        string m1 = m.Substring(m.IndexOf("<SelectObj1>")- ("<SelectObj1>").Length+5, a);
                        node.InnerText = m.Insert(m.IndexOf("<SelectObj1 >")+ ("<SelectObj1 >").Length+ a+82+ m.IndexOf("</SelectObj1 >") + ("</SelectObj1 >").Length, m1);
                    }
                }
                xmlDocument.Save(@"C \test1.xml");
            
            }
    }
    

    Result:


    Hope my solution could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 15, 2019 9:49 AM
    Moderator