Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
  • XML

    • Better ways to Encode HTML in C#

      11 Dec 2009 by Dean / No Comments

      Microsoft provides lots of different ways to encode HTML and URIs, but their newest library, called AntiXSS, is the best of the bunch.

      Continue Reading

    • Using WCF to return HTML

      18 Feb 2009 by Dean / 1 Comment

      I just answered a WCF question on StackOverflow, and decided it was worth cross-posting here as well.

      The question was: What is the best / most flexible way to have WCF output XHTML?

      Here’s how we do it at Infovark. While I’m not sure that our approach is the best way, it does the job.

      Our approach is to use the DataContractSerilizer to generate XML, then apply a Complied XSLT transform and return the result stream, which should now contain XHTML. Here’s a simplified version of our code:

      1.     public Stream GetItemAsHtml(string id) {
      2.         Item obj = GetItem(objectId);
      3.         Stream xml = GetXmlStream(obj);    
      4.         return TransformXmlStream(xml, defaultTransform);
      5.     }        
      6.  
      7.     public static Stream GetXmlStream(IXmlSerializable item) {
      8.         MemoryStream stream = new MemoryStream();
      9.         using (XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings { Encoding = Encoding.UTF8 })) {
      10.             if (writer != null) {
      11.                 DataContractSerializer dcs = new DataContractSerializer(item.GetType());
      12.                 dcs.WriteObject(writer, item);
      13.  
      14.                 writer.Flush();
      15.                 writer.Close();
      16.             }
      17.         }
      18.         stream.Seek(0, SeekOrigin.Begin);
      19.         return stream;
      20.     }
      21.  
      22.     public static Stream TransformXmlStream(Stream xml, string xsltFile) {
      23.         XmlReader reader = XmlReader.Create(xml);
      24.  
      25.         XslCompiledTransform trans = new XslCompiledTransform();
      26.         trans.Load(xsltFile);
      27.  
      28.         MemoryStream stream = new MemoryStream();
      29.         using (XmlWriter writer = XmlWriter.Create(stream, trans.OutputSettings)) {
      30.             if (writer != null) {
      31.                 trans.Transform(reader, writer);
      32.  
      33.                 writer.Flush();
      34.                 writer.Close();
      35.             }
      36.         }
      37.         stream.Seek(0, SeekOrigin.Begin);
      38.         return stream;
      39.     }

      It works for us, but if you’ve got other, better ideas, please let me know!

      Continue Reading

    • Categories

      • .NET (41)
      • AJAX (3)
      • Books (7)
      • HTML (9)
      • Infovark (8)
      • Programming (48)
      • REST (11)
      • SQL (3)
      • Testing (3)
      • Tools (13)
      • UI (3)
      • WCF (11)
      • Web Services (8)
      • WPF (4)
      • XML (4)
    • Archives

    • Get future articles


       

    • Blogroll

      • Ajaxian
      • Anne Van Kesteren
      • Brain.Save()
      • Coding Horror
      • Eric Sink
      • Joel Spolsky
      • John Resig
      • Mark Pilgrim
      • Raymond Chen
      • Scott Hansleman
      • Secret Geek
      • Steve Yegge
      • The Daily WTF
      • The Database Programmer
    • Meta

      • Log in
      • Entries RSS
      • Comments RSS
      • WordPress.org
  • Site map

    • News
    • Product
    • Download
    • Buy
    • Support
    • About
  • Recent Posts

    • Review: Brownfield Application Development in .NET
    • Using Modal Dialogs with a Splash Screen in WPF
    • Highlighting query terms in a WPF TextBlock
    • Getting XAML Hyperlink text to wrap
    • How to format the XAML Hyperlink NavigateUri
  • Twitter

    Copyright 2011 Infovark, Inc. All rights reserved.