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 7524377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:05:10+00:00 2026-05-30T03:05:10+00:00

In my C# app, XML data may contain arbitrary element text that’s already been

  • 0

In my C# app, XML data may contain arbitrary element text that’s already been pre-processed, so that (among other things) illegal characters have been converted to their escaped (xml character entity encoded) form.

Example: <myElement>this & that</myElement> has been converted to <myElement>this &amp; that</myElement>.

The problem is that when I use XmlTextWriter to save the file, the ‘&’ is getting re-escaped into <myElement>this &amp;amp; that</myElement>. I don’t want that extra &amp; in the string.

Another example: <myElement>• bullet</myElement>, my processing changes it to <myElement>&#8226; bullet</myElement> which gets saved to <myElement>&amp;#8226; bullet</myElement>. All I want output to the file is the <myElement>&#8226; bullet</myElement> form.

I’ve tried various options on the various XmlWriters, etc but can’t seem to get the raw strings to get output correctly. And why can’t the XML parser recognize & not rewrite already a valid escapes?

update: afer more debugging, I found that element text strings (actually all strings including element tags, names, attributes, etc. ) get encoded whenever they get copied into the .net xml object data (CDATA being an exception) by an internal class called XmlCharType under System.Xml. So the problem has nothing to do with the XmlWriters. It looks like the best way to solve the problem is to un-escape the data when it’s output, either by using something like:

string output = System.Net.WebUtility.HtmlDecode(xmlDoc.OuterXml);

Which will probably evolve into a custom XmlWriter in order to preserve formatting, etc.

Thanks all for the helpful suggestions.

  • 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-30T03:05:10+00:00Added an answer on May 30, 2026 at 3:05 am

    Ok, here’s the solution I came up with:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Versioning;
    using System.Text;
    
    namespace YourName {
    
        // Represents a writer that makes it possible to pre-process 
        // XML character entity escapes without them being rewritten.
        class XmlRawTextWriter : System.Xml.XmlTextWriter {
            public XmlRawTextWriter(Stream w, Encoding encoding)
                : base(w, encoding) {
            }
    
            public XmlRawTextWriter(String filename, Encoding encoding)
                : base(filename, encoding) {
            }
    
            public override void WriteString(string text) {
                base.WriteRaw(text);
            }
        }
    }
    

    then using that as you would XmlTextWriter:

            XmlRawTextWriter rawWriter = new XmlRawTextWriter(thisFilespec, Encoding.UTF8);
            rawWriter.Formatting = Formatting.Indented;
            rawWriter.Indentation = 1;
            rawWriter.IndentChar = '\t';
            xmlDoc.Save(rawWriter);
    

    This works without having to un-encode or hack around the encoding functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a asp.net web forms app that uses System.Web.Caching.Cache to cache xml data
I have a Grails app that loads its data from xml files and delivers
I have to import some XML data into my app. Now I open a
In my app i have to send a xml data as request, I am
I'm currently working on a Silverlight app and need to convert XML data into
I use URLLoader to load data into my Flex app (mostly XML) and my
I’m making a simple LOB app which loads data from an XML file and
I'm creating an asp.net app with just some lite data access from xml files.
Scenario: I'm working on a rails app that will take data entry in the
I'm working on a utility web app that to help manipulate some domain-specific XML

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.