atatko
atatko

Reputation: 481

How do I serialize an object with a string value that contains an ampersand?

I am trying to create a table using Xml/Xslt, where some of the cells are empty, but IE formats the empty cell as invisible. Here is an example of a cell object that is a pretty simplified version of what I'm dealing with:

    public class Cell
    {
         public value;

         public Cell(string value){ this.value = value; }
    }

So if I have a bunch of cells in a table, I'm setting value =   when a given cell is empty or null, so that displaying that cell in IE will show an empty cell rather than no cell at all.

The problem is that the string   is always translated to   when I serialize the object.

How do I fix this?

Upvotes: 2

Views: 2034

Answers (2)

Gustavo F
Gustavo F

Reputation: 2206

Simply use the HttpUtility methods to decode your string after the serialization. i.e:

using System;
using System.Web;

namespace htmlencode
{
    class Program
    {
        static void Main(string[] args)
        {
            var encoded = HttpUtility.HtmlEncode(" ");
            var decoded = HttpUtility.HtmlDecode(" ");
            Console.WriteLine("Encoded: " + encoded); //Prints  
            Console.WriteLine("Decoded: " + decoded); //Prints  
            Console.ReadLine();
        }
    }
}

Upvotes: 0

SLaks
SLaks

Reputation: 888107

You should use the actual character ("\u0160").
The XML serializer should correctly entitize that.

Upvotes: 3

Related Questions