XmlSerializer does not encode special characters like &

I have a class named Config that I would like to serialize to XML using 
XmlSerializer. That class has a couple of string properties that may contain 
special characters like <, >, &, etc. I am currently using the following 
method to serialize the instance (m_Config) of the class:

Private Sub SerializeConfiguration()
    Dim userStorage As IsolatedStorageFile = 
IsolatedStorageFile.GetUserStoreForAssembly
    Dim fileStream As IsolatedStorageFileStream = New 
IsolatedStorageFileStream("Config.XML", IO.FileMode.Create, userStorage)
    Dim serializer As XmlSerializer = New XmlSerializer(GetType(Config))
    serializer.Serialize(fileStream, m_Config)
    fileStream.Close()
End Sub

The problem with this method is that special characters (e.g. <, >, &) are 
not encode (e.g. to <, >, &), so the output is not well-formed XML.

Is there an easy and proper way to make XmlSerializer encode special 
characters. I do not want to encode the data inside the instance of my Config 
class.

Thanks and kind regards
Sebastian
0
10/15/2009 3:31:32 PM
dotnet.xml 7266 articles. 0 followers. Follow

3 Replies
2481 Views

Similar Articles

[PageSpeed] 39

Sebastian Daser schrieb:

> The problem with this method is that special characters (e.g. <, >, &) are 
> not encode (e.g. to <, >, &), so the output is not well-formed XML.

'<' and '>' are enclosing characters of the tags. Having these in the
content might mix up tag hierarchy.

Quote from http://en.wikipedia.org/wiki/Xml#Characters_and_escaping
| The characters "<" and "&" are key syntax markers and may never appear in content

> Is there an easy and proper way to make XmlSerializer encode special 
> characters. 

I guess not. I have seen the same behaviour.

CU,
Christian
0
10/15/2009 5:13:46 PM
Sebastian Daser wrote:
> I have a class named Config that I would like to serialize to XML using 
> XmlSerializer. That class has a couple of string properties that may contain 
> special characters like <, >, &, etc. I am currently using the following 
> method to serialize the instance (m_Config) of the class:
> 
> Private Sub SerializeConfiguration()
>     Dim userStorage As IsolatedStorageFile = 
> IsolatedStorageFile.GetUserStoreForAssembly
>     Dim fileStream As IsolatedStorageFileStream = New 
> IsolatedStorageFileStream("Config.XML", IO.FileMode.Create, userStorage)
>     Dim serializer As XmlSerializer = New XmlSerializer(GetType(Config))
>     serializer.Serialize(fileStream, m_Config)
>     fileStream.Close()
> End Sub
> 
> The problem with this method is that special characters (e.g. <, >, &) are 
> not encode (e.g. to <, >, &), so the output is not well-formed XML.

Are you posting using the web interface to the newsgroups?
Unfortunately discussing encoding questions that way is difficult as any 
entity references you might have used are not showing up.

> Is there an easy and proper way to make XmlSerializer encode special 
> characters. I do not want to encode the data inside the instance of my Config 
> class.


I don't see that problem.
With e.g.

     class Program
     {
         static void Main(string[] args)
         {
             Foo foo1 = new Foo() { Bar = "a < b && b < c" };
             XmlSerializer ser = new XmlSerializer(typeof(Foo));
             ser.Serialize(Console.Out, foo1);
             Console.WriteLine();
         }
     }

     public class Foo
     {
         public string Bar { get; set; }
     }

the characters '<' and '&' are properly escaped
(as '& l t;' and '& a m p;', only without the spaces I inserted in the 
hope the web interface shows what I want).

-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
mahotrash (1777)
10/15/2009 5:37:58 PM
I have found out now.... I need to use XmlTextWriter.

My code now looks like:

Private Sub SerializeConfiguration()
    Dim userStorage As IsolatedStorageFile = 
IsolatedStorageFile.GetUserStoreForAssembly
    Dim fileStream As IsolatedStorageFileStream = New 
IsolatedStorageFileStream("Config.XML", IO.FileMode.Create, userStorage)
    Dim xmlTextWriter as new XmlTextWriter(fileStream, Text.Encoding.Unicode)
    Dim serializer As XmlSerializer = New XmlSerializer(GetType(Config))
    serializer.Serialize(xmlTextWriter, m_Config)
    fileStream.Close()
End Sub


"Sebastian Daser" wrote:

> I have a class named Config that I would like to serialize to XML using 
> XmlSerializer. That class has a couple of string properties that may contain 
> special characters like <, >, &, etc. I am currently using the following 
> method to serialize the instance (m_Config) of the class:
> 
> Private Sub SerializeConfiguration()
>     Dim userStorage As IsolatedStorageFile = 
> IsolatedStorageFile.GetUserStoreForAssembly
>     Dim fileStream As IsolatedStorageFileStream = New 
> IsolatedStorageFileStream("Config.XML", IO.FileMode.Create, userStorage)
>     Dim serializer As XmlSerializer = New XmlSerializer(GetType(Config))
>     serializer.Serialize(fileStream, m_Config)
>     fileStream.Close()
> End Sub
> 
> The problem with this method is that special characters (e.g. <, >, &) are 
> not encode (e.g. to <, >, &), so the output is not well-formed XML.
> 
> Is there an easy and proper way to make XmlSerializer encode special 
> characters. I do not want to encode the data inside the instance of my Config 
> class.
> 
> Thanks and kind regards
> Sebastian
0
10/19/2009 6:30:06 PM
Reply:

Similar Artilces:

What's the easiest way to add a character to the beginning of all cells?
Hello I would like to know what's the easiest way to add a specaial character to the beginning of all the cells in a selected area in Excel. For example I have these records in cells A1 to A3: 2*3 2*4 2*5 Now I want them to be something like this: =2*3 =2*4 =2*5 (I want to convert them into formulas) How can I do this? I know some complex ways to do this such as putting a "=" character in "B1 to B3" and then writing this formula in C1 to C3: "=A1&B1" and then... or I can also write a macro for this, but I'm looking for a very simple and easy way. I...

HELP
Hi, One of the columns in my excel has its content broken into lines. I guess each line within that column ends with a carrieage return and line feed. how can I replace these with say a "-" character? I was trying to use Find and Replace but don't know how to enter the CRLf character and if I use Alt 13 and / or Alt 10 (for cariage return and line feed) it wont fine these. Any help will be appreciated Dino has responded to his initial post. Dino Buljubasic wrote: > > Hi, > One of the columns in my excel has its content broken into lines. I > guess each line ...

Use cell like a checkbox
Hi experts, I'm wondering, is there a way to format a cell so that it reacts like a checkbox? I want to simply toggle the content of a cell between "x" (selected) and " " (empty= not selected) simply by clicking on a certain cell. Thanks for your help! Rene You need VBA: You can set-up a double-click event macro to perform this function. -- Gary''s Student "Rene" wrote: > Hi experts, > > I'm wondering, is there a way to format a cell so that it reacts like a > checkbox? > > I want to simply toggle the content of a cell...

how can i susbstitute the symboals like '-' or��#�� in the cell? #2
hi, till now ,the excel only support substitute the text in the cell,as we can use SUBSTITUE function. But if i want to substitue the symboals like ��-�� or��#'��how can i do? Regards, Chelsea ...

Create row lower border that looks like "accountant single" underline?
I'm trying to format a spreadsheet so that a row has a lower border that looks like the "accountant single" underline - something like this: ___________ __________ _________ ________ instead of a continuous line like this: ________________________________________ Is it possible to do this as a border? The accountant-single underline (applied as a font attribute) only displays if there is data in the cell, and I want it to be there whether or not there is any data. Many thanks for any help with this. Edward Mendelson "Edward Mendelson" <edward_mendelson@z...

Unable to translate Unicode character \u00E9 at index 5409 to specified code page.
I've got some C# 2.0 code that has been working for a year. using (XmlWriter w = XmlWriter.Create("out.xml" ,settings)) { // many lines of code to write to w w.WriteStartElement("contactTypeRef"); Suddently, I'm getting this 100% repeatable error: ************** Exception Text ************** System.Text.EncoderFallbackException: Unable to translate Unicode character \u00E9 at index 5409 to specified code page. at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index) at System.Xml.CharEntityEncoderFallbackBuffer.Fallback(Char...

DELETING THE LAST CHARACTER IN A STRING
HOW DO YOU DELETE THE LAST CHARACTER IN A STRING?? I HAVE SOME ITEM ID AND NEED TO DELETE THE LAST CHARACTER EXAMPLE: C-1202A NEDD TO MAKE IT C-1202 John, Try this: =LEFT(A1,LEN(A1)-1) John "JOHN ALMEDA" <jalmeda@northsouth.net> wrote in message news:254B4DCA-5737-4F45-A890-6965D1514909@microsoft.com... > HOW DO YOU DELETE THE LAST CHARACTER IN A STRING?? > > I HAVE SOME ITEM ID AND NEED TO DELETE THE LAST CHARACTER > > EXAMPLE: C-1202A NEDD TO MAKE IT C-1202 Try this with the string in a1 =LEFT(A1,LEN(A1)-1) -- Regards Ron de Bruin (Win XP Pro SP-1 ...

How to administer exchange to accept GB18030 Charset encoded messa
Hello, We received this message from exchange: "This message has been rejected by the Exchange organization to which it was sent because the message was encoded using the GB18030 Charset" What can I do to configure Exchange so that it accepts messages of this type? Thanks, Peter http://www.microsoft.com/globaldev/DrIntl/columns/015/default.mspx On Tue, 31 Aug 2004 17:11:02 -0700, "Peter" <Peter@discussions.microsoft.com> wrote: >Hello, > >We received this message from exchange: > >"This message has been rejected by the Exchange organizat...

how do i make 1/3 look like a fraction
i can get 1/4 and 1/2 to look like a fraction but the rest (1/3 1/8 1/16) don't happen There are a couple of possibilities - see: http://www.gmayor.com/createfraction.htm or http://www.word.mvps.org/FAQs/Formatting/CreateFraction.htm -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<...

Would like to delete a folder I added to Folder List in Outlook 2002
HELP says: 1. In the Folder List, click the folder you want to delete. 2. On the File menu, point to Folder, and then click Delete 'Folder Name'. After clicking the folder I want to delete and going to File | Folder, there is no delete option available. There is a Delete Outlook Today option, but it is grayed out. How can I delete the folder I added? TIA I always use right click, delete.... Are you using the folder list or the outlook bar? -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual Quick...

Can I create a custom control with a designer like a form designer
Using Visual Studio 2008, is there anyway I can create a custom control which is comprised of several standard windows controls put together in a certain way? For example, I want to have a panel, and inside this panel will be a few other Panels with other controls within these panels. I want to position them, set their docking and create a basic interface which would be grouped together as one component. Then I would want to go back to my main app and be able to add these components throughout my app - just drag n drop in on some location set it's docking and be done,...

Garbled text/characters in Outlook 2007
I have Vista/Office 2007 Ultimate/Outlook 2007 installed. I have a paid hotmail acct. Recently upgraded fm 2003 Outlook to 2007 and now when I receive emails in Outlook 2007, I get garbled characters like Chinese/Japanese characters with question marks, etc etc in the text. Previously I had no such problems. I am not using Hotmail connector now. All help appreciated. Thanks -- Stephen Sar You do need the Outlook Connector and should install it, but you should also check your encoding settings in the garbled emails. Switch to another type of encoding to see if that works. Paid Hotmail...

Limit Unbound Field to One Character
Hi, How can I limit the length of an unbound text box to one character in length? I only want a user to enter one character. Thanks, Anthony -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-formscoding/200708/1 See: Unbound text box: limiting entry length at: http://allenbrowne.com/ser-34.html -- Allen Browne - Microsoft MVP. Perth, Western Australia Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org. "biganthony via AccessMonster.com" <u31673@uwe> wrote in...

Format this cell like that cell, automatically?
I keep my grades in an Excel workbook of four sheets, of which the two relevant ones are Attendance and Grades. During the semester, if a student drops the course I mark all cells for that student in both worksheets as Locked and I apply a gray pattern to them. Having to do this in two worksheets is kind of a nuisance. Is there any way to set, say cells Grades!A11:AP11 to pick up automatically any formatting and Lock/Unlock status as it changes in Attendance!A1? I know I could do this in a macro, but I'm wondering if there's any purely worksheet way to do it. -- Stan Brown, O...

@ character
Does the @ character hold any functional significance in Excel? I can' seem to enter any @text. It returns that the function is invalid. came acrozz a file that already had cells with @text but I can't see to use the find.replace function on those. Help, please. Thanks -- Prosperin ----------------------------------------------------------------------- Prosperina's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=1452 View this thread: http://www.excelforum.com/showthread.php?threadid=26150 Prosperina Lotus used the @ as a start for functions and Exc...

Special Orders and Customer Information
Ok I've been on the phone with Microsoft on this one and they are of no help. I am in the Juvenile Furniture Retail Industry. Scenario: When I create an order for a customer on an item that we are currently out of stock on but normally carry. For example an Oak dresser. We have 10 of these dressers coming in. How can I reference this sale to one of these dressers that are coming in? I know that if I sold it in full I will be at a -1 On Hand or if I create a Layaway for this customer I will have 1 Committed. But I still don't see a way to easily tag one of these dressers for the ...

Increase distribution reference field to at least 50 characters.
My client uses the distribution reference field which appear to be very long but only allows 30 characters. They would great appreciate having the field be at least 50 characters long. -- Thanks a lot, Julie London Technology Management Concepts (TMC) ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "...

Wild Characters in "IF" function
I've seen in this forum that someone already asked the question bu there was no answer... I'm also unable to use the wild character (*) in my IF function - want the formula to return $51.44 and $25.31 amounts in column D i "EXON" chain is encountered in cells of column B. My statement look like this: =IF(B4="EXXON*",C4,0) but it doesn't work... Calumn A Column B Column C Column D 10/29/2005 HEINIE'S MARKET $12.28 10/30/2005 VAIL PASS SALES 0374 $198.00 10/29/2005 EINSTEIN BROS #1793 $...

I would like to make a schedule for my dept. Who works what days .
I am supervisor for a nursing dept. I need to make a schedule for them to follow as to who works when and what shift. I need to be sure they all get the hours that they are required to work. Is there a template that would help me and be able to sort things out in the schedule as well? ...

Another "mailto" question -- mailto link that puts body of emails like "reply" does?
This has come up again so thought I'd ask if there was code one could add to the "mailto" link to start new message that would add the email bodies in? I know this one is a long-shot, but since it's a 50/50 type of deal, thought I'd ask anyway. I need to do this when I send an email from home but that the recipient might like to respond to to my work email address. Up till now, I've just put a blurb with a "mailto" with my office email address and left it at that. I'm hoping to get a little more sophisticated, if that is indeed possible. Hopefully s...

Help with special Excel mail merge...
Hi. I'm looking for a way to replace some variable names in an HTML file with row data from an Excel spreadsheet. I would like to create one HTML file per row of data. I tried doing this using the mail merge feature of Microsoft Word and was pleased until I ran into two problems: 1. The MS Word mail merge does not substitute an image in place of an image name. 2. The final results are printed, whereas I would like each row result to be saved into a separate file. Is there any app (preferably freeware) that can extract Excel row data and replace each specified HTML variable with...

Word-like "track changes" for excel
Is there a track changes for excel that is more "word-like". I'd lik to see both old and new changes on screen at the same time. Any 3rd party or macros that might accomplish this?? Thanks -- jcm122 ----------------------------------------------------------------------- jcm1229's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=1484 View this thread: http://www.excelforum.com/showthread.php?threadid=26617 ...

Extracting data to the right of a character
I need to extract data to the right of a dash (-). For example, from 123-34, I need 34 The data can be of a variable length with no set length either before or after the dash so I can't use Right or Left functions. I can't immediately see a built-in function that would do this. Do I have to grow my own? Anyone already done it? TVM Anthony You need to use the Instr function to locate the dash and then get everything from there on. Mid([YourField],Instr(1,[YourField],"-")+1) Actually if YourField could be Null, you might want to force the Instr comparison to check a s...

Lost Character on the Keyboard
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: Intel Hi! <br><br>I couldn't find anything on a similar subject from the other forums, so decided to ask the question myself. <br><br>The problem is - one of my characters stopped working in MS Word 2008 one day, namely '�'. <br><br>I use an Estonian keyboard with custom characters such as �, �, �, �, the last one of which my Word app suddenly started to ignore. Whenever I press the corresponding key, it simply moves the insertion point up one line. The character doesn'...

Using a chart like a formula...?
Hey, I want to track the progress of certain "students". I have their results from tests that they sit in a register-style worksheet. (Names are in column A, and scores are under appropriate heading in row 1.) If I was to create a formula to say add SCORE ONE + SCORE TWO + SCORE THREE... I would create a formula that looked like; =SUM(B2:B26). I would then want that to appear in each row, so I would use the tiny square in the bottom right hand corner and it would copy down the formula using dynamic cell references that were corresponding. All fairly simple stuff. I would li...