0

I have following xml document

<Node id="1" name="name1" autoplayTrailer="false" visible="true" bgimages="false">
      <Text>
        <title id="2"  type="text" hideineditor="false" visible="true"><![CDATA[Link]]></title>
        <sections id="3"  default="consideration">         
          <songs id="4"  type="text" hideineditor="false" enabled="true" visible="true">
            <![CDATA[
              <div class="ThumbContainer">
                Some text here
              </div>
            ]]>         
            <songsTitle><![CDATA[sometexthtml here]]></songsTitle>
          </songs>
           </sections>
     </Text>
</Node>

I want to read the content/node one by one and modify the CDATA content and write the xml to disc.

Problem is i am not able to write the CData for <songs> node becoz it has another node inside <songTitle> node without closing the </song> node is it possilbe to write node with CData having another node following CData content?

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
  • What API are you using? LINQ-to-XML? The older `XmlDocument`? `XmlSerializer`? Directly reading using `XmlReader` (or hopefully not)? – dbc Dec 09 '19 at 06:18
  • XmlTextWriter to write element..and StreamReader to read the xmlstring – SHEKHAR SHETE Dec 09 '19 at 06:21
  • That's a very low-level API you're using. Can you share a minimal example of the code you have so far, and where you're stuck? As an aside, using LINQ to XML might be easier as long as you can load the entire XML into memory. – dbc Dec 09 '19 at 06:36

2 Answers2

0

Try xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement node in doc.Descendants("Node"))
            {
                XElement songs = node.Descendants("songs").FirstOrDefault();
                XCData child = (XCData)songs.FirstNode;
                string childStr = child.ToString();
                childStr = childStr.Replace("Some text here", "Some text there");
                child.ReplaceWith(childStr);
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Is this the output you want to create?

This example outputs the CData & 'other element' under the 'songs' element using the XmlTextWriter API.

using System;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WriteCData
{
class Program
{
    static void Main(string[] args)
    {
        // some output to write to
        XmlTextWriter xtw = new XmlTextWriter(@"c:\temp\CDataTest.xml", null);

        // start at 'songs' element
        xtw.WriteStartElement("songs");
        xtw.WriteCData("<div class='ThumbContainer'>Some text here</div>");
        xtw.WriteStartElement("songsTitle");
        xtw.WriteCData("sometexthtml here");
        xtw.WriteEndElement();      // end "songTitle"
        xtw.WriteEndElement();      // end "songs"

        xtw.Flush();            // clean up
        xtw.Close();
    }
}
}

output:

<songs>
<![CDATA[<div class='ThumbContainer'>Some text here</div>]]>
<songsTitle><![CDATA[sometexthtml here]]></songsTitle>
</songs>

The idea is to replace the static text used in the example with the values you read from your source document you mention in your question.

Marvin Smit
  • 4,088
  • 1
  • 22
  • 21