Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Xml writing quotes etc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sagaert Johan - 07 Mar 2008 13:52 GMT
Hi

Is there some kind of method to convert strings containing " <   >  etc  to
some form that it is allowed inside XML tags ?

Johan
Martin Honnen - 07 Mar 2008 14:02 GMT
> Is there some kind of method to convert strings containing " <   >  etc  to
> some form that it is allowed inside XML tags ?

Use XmlWriter to create your XML, it does the necessary escaping for
you. If you only want to create a text then use XmlWriterSettings with
ConformanceLevel.Fragment e.g.

      XmlWriterSettings settings = new XmlWriterSettings();
      settings.ConformanceLevel = ConformanceLevel.Fragment;
      StringWriter writer = new StringWriter();
      using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
      {
        xmlWriter.WriteString("a < b && b < a");
      }
      Console.WriteLine(writer.ToString());

writes "a &lt; b &amp;&amp; b &lt; a".

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Jon Skeet [C# MVP] - 07 Mar 2008 14:07 GMT
> Is there some kind of method to convert strings containing " <   >  etc  to
> some form that it is allowed inside XML tags ?

The best approach is to use an API which lets you build up the XML
document just by specifying the strings, and it will do the escaping
for you - XmlWriter, XmlDocument etc.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.