C#: deserialize XML with special characters

C#
Copy to clipboard
fablecode
  1. // Source: http://www.webr2.com/fast-way-to-deserialize-xml-with-special-characters/
  2.  
  3. string requestUrl = "http://urlwithxml"
  4.  
  5. using (var client = new HttpClient())
  6. {
  7.         using (var response = await client.GetAsync(requestUrl))
  8.         {
  9.                 var result = await response.Content.ReadAsStringAsync();
  10.  
  11.                 var encoding = new UTF8Encoding();
  12.                 var byteArray = encoding.GetBytes(result);
  13.  
  14.                 using (var memoryStream = new MemoryStream(byteArray))
  15.                 {
  16.                         var xmlSerializer = new XmlSerializer(typeof(T));
  17.                         var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
  18.  
  19.                         return (T)(xmlSerializer.Deserialize(xmlTextWriter.BaseStream));
  20.                 }
  21.  
  22.         }
  23. }

    Leave a comment

     
    Visual verification
     

    Related books

    More InfoLearn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    Learn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    More InfoC#: Programming Basics for Absolute Beginners (Step-By-Step C#) (Volume 1)
    C#: Programming Basics for Absolute Beginners (Step-By-Step C#) (Volume 1)
    More InfoC# Programming: THE NO-NONSENSE GUIDE: Learn C# Programming Within 12 Hours!
    C# Programming: THE NO-NONSENSE GUIDE: Learn C# Programming Within 12 Hours!
    More InfoThe C# Player's Guide (3rd Edition)
    The C# Player's Guide (3rd Edition)
    More InfoMicrosoft Visual C# Step by Step (8th Edition) (Developer Reference)
    Microsoft Visual C# Step by Step (8th Edition) (Developer Reference)
    More InfoThe C# Programming Yellow Book
    The C# Programming Yellow Book
    More InfoC# Programming in easy steps
    C# Programming in easy steps
    More InfoC# 6.0 and the .NET 4.6 Framework
    C# 6.0 and the .NET 4.6 Framework