简体   繁体   中英

decoding cdata content within xml

A customer has sent us an XML file with CDATA content that is XML encoded ie <![CDATA[some content]]>

What is the best way in asp.net to replace the content in the XML file with a decoded version? (without asking the customer to send us a correct file)

thanks

This might not be what you are looking for, but it will at least give you a start:

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

namespace CSSandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            string oldXml = "<root><child>No CDATA here</child><child><![CDATA[Illegal xml & <> '' bobby tables]]></child><child><child><![CDATA[More CDATA &&&]]></child></child></root>";
            Console.WriteLine(oldXml);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(oldXml);

            ProcessNodes(doc, doc.ChildNodes);

            string newXml = doc.OuterXml;
            Console.WriteLine(newXml);

            Console.ReadLine();
        }
        static void ProcessNodes(XmlDocument doc, XmlNodeList nodes)
        {
            foreach (XmlNode node in nodes)
            {
                if (node.HasChildNodes)
                {
                    ProcessNodes(doc, node.ChildNodes);
                }
                else
                {
                    if (node is XmlCDataSection)
                    {
                        string cdataText = node.InnerText;
                        node.ParentNode.InnerXml = SecurityElement.Escape(cdataText);
                    }
                }
            }
        }
    }
}

This assumes that your cdata block is the only child of the current node (as per my test).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM