开发者

decoding cdata content within xml

开发者 https://www.devze.com 2023-04-02 02:37 出处:网络
A customer has sent us an XML file with CDATA content that is XML encoded i.e. <![CDATA[some content]]>

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

What i开发者_运维技巧s 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号