How to deserialize (parse) XML from a string

An easy function that returns the deserialized version from a XML document that was stored in a string.


Libraries
using System.Xml;
using System.IO;
using System.Xml.Serialization;

Code Snippet
public yourXmlClassesType ParseXML(string xmlFile) {
    var xml = xmlFile;
    var serializer = new XmlSerializer(typeof(yourXmlClassesType));
    using (var reader = new StringReader(xml)) {
        yourXmlClassesType parsedContent = (yourXmlClassesType)serializer.Deserialize(reader);
        return parsedContent;
    }
}


Implementation

To use it imagine you have the serialized classes (Read more here: http://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part) you have to modify the function above. So replace "yourXmlClassesType" to the name of your final masterclass. That's it!





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    XML
    parse



    Informations

    posted on 2016-10-23 15:45:34

    viewed 41 times

    snippet's UUID is 580cbefeb41e4

    Similar Snippets