Syntax_MM
Syntax_MM

Reputation: 53

C# XML how to stop & from converted to &

This is regarding C# VS 2012

using System.Xml.Linq;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            XElement xe = new XElement("entry", "&27&");
            Console.WriteLine(xe);
        }
    }
}

My user wants some bizarre entries in his xml input file where it involves 2 &s so he can execute his Linux shell scripts. To keep this simple, when I do the above, I get this output:

<entry>&amp;27&amp;</entry>

but I want this output instead:

<entry>&27&</entry>

From what I've read, a single & will causing incorrect XML syntax so VS do this on purpose but I have 2 &s. My user won't take

<entry>&amp;27&amp;</entry> 

as input, he wants "&27&" so I'm stuck, please help.

Upvotes: 0

Views: 380

Answers (1)

martijn
martijn

Reputation: 495

It all depends on how he is reading the xml that you provided. As in your example the print on the console will show the escaped & character as you are printing the xml. If you print the value of the Xelement Console.Writeline(xe.value); you will see the expected outcome.

Your xml is completely perfect and as long as your customer is parsing the xml file with a proper xml parser he is able to get the attribute values in the desired format and use it.

Upvotes: 1

Related Questions