Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 227627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:36:51+00:00 2026-05-11T19:36:51+00:00

I want there to be CDATA sections in my output XML. I have tried

  • 0

I want there to be CDATA sections in my output XML. I have tried to use the “cdata-sections-element” attribute of xsl:output. However, I don’t get CDATA in my output.

using System;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string stylesheet =
@"<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
<xsl:output xmlns:t=""temp-uri"" cdata-section-elements=""t:text"" method=""xml""/>
<xsl:template match=""/"">
<text xmlns=""temp-uri""><xsl:value-of select="".""/></text>
</xsl:template>
</xsl:stylesheet>
";
            XmlReader reader = XmlReader.Create(new StringReader(stylesheet));
            XslCompiledTransform t = new XslCompiledTransform(true);
            t.Load(reader);

            XmlReader input = XmlReader.Create(new StringReader("<foo><![CDATA[<hello]]></foo>"));
            StringBuilder sb = new StringBuilder();

            XmlWriter results = XmlWriter.Create(new StringWriter(sb));
            t.Transform(input, null, results);

            Console.WriteLine(sb.ToString());
            Console.ReadLine();
        }
    }
}

Actual output

 <?xml version="1.0" encoding="utf-16"?><text xmlns="temp-uri">&lt;hello</text>

Required output

<?xml version="1.0" encoding="utf-16"?><text xmlns="temp-uri"><!CDATA[<hello]]></text>

What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:36:51+00:00Added an answer on May 11, 2026 at 7:36 pm

    Your XmlWriter has to be informed about the current output settings:

    XmlWriter results = XmlWriter.Create(new StringWriter(sb), t.OutputSettings);
    //---------------------------------------------------------^^^^^^^^^^^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file with some CDATA nodes. I want to change the
I have a XML in which there is another XML (within CDATA). Now I
I have an Ul of item. I want to alternate there background color here
I have an xml that i am parsing there is a field that contains
I have an Xml like this: <root> <item> <content type=HTML> <![CDATA[<html> <head></head> <body>something goes
say I have an XML file like: <library> <books> <![CDATA[<genre><name>Sci-fi</name><count>2</count></genre>]]> <book> <name> Some Book
I have an XML object that I want to send to my player via
today I want to try parsing an xml data from an URL. I use
I have a file which contains corrupt XML, There are some garbage characters at
i want to know there a limitation in size for an iphone to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.