How to replace special char in Xml with C#

Posted By  RRaveen On 03 Jul 2010 03:07:45
emailbookmarkadd commentsprint
No of Views:1387
Bookmarked:0 times
Votes:0 times

Introduction

In this tips, I will describe, how to replace the special character in xml. Because few  no of char not work in XML or used for xml tag composing.

  • Ampersand         &     &    
  • Left angle bracket     &lt;     <    
  • Right angle bracket     &gt;     >    
  • Straight quotation mark &quot;     "    
  • Apostrophe         &apos;    '

Implementation

In the .NET introduced the new class to escape special chars easily. The class is SecurityElement. This is class has few no of methods. But here we are going to access Escape method.

Method Definition

//// Summary://     Replaces invalid XML characters in a string with their valid XML equivalent.//// Parameters://   str://     The string within which to escape invalid characters.//// Returns://     The input string with invalid characters replaced.public static string Escape(string str);

CodeSnippet

private string FormatForXML(object input)
    {return SecurityElement.Escape(input.ToString());
    }

This is straight forward method, just we have to pass whatever string inside the escape method, the .NET do the necessary replacement and return to us.That's all, Just call copy this function and use it your project.Thank you for reading.

Sign Up to vote for this article
Other popular Tips/Tricks
    Comments
    By:RRaveenDate Of Posted:9/9/2010 7:09:11 AM
    Like this way
    You have to use protected override void OnPaint(PaintEventArgs e) { // here you have to write ur code. } if you have any questions, please ask in codegain message board to get faster response. thank you
    By:NicoDate Of Posted:9/9/2010 5:13:03 AM
    re
    Hi, where I call this function and how I get the Graphics parameter? Under .NET CF I do not have any OnPaint event or something like that. Thanks
    Leave a Reply
    Title:
    Display Name:
    Email:
    (not display in page for the security purphase)
    Website:
    Message:
    Please refresh your screen using Ctrl+F5
    If you can't read this number refresh your screen
    Please input the anti-spam code that you can read in the image.
    ^ Scroll to Top