Deserializing XML from String

119,251

Solution 1

Try this:

string xml = "<StatusDocumentItem xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;

using (TextReader reader = new StringReader(xml))
{
    result = (StatusDocumentItem)serializer.Deserialize(reader);
}

Console.WriteLine(result.Message);
Console.ReadKey();

Does it show "Job put in queue"?

Solution 2

This generic extension works well for me....

public static class XmlHelper
{
    public static T FromXml<T>(this string value)
    {
        using TextReader reader = new StringReader(value);
        return (T) new XmlSerializer(typeof(T)).Deserialize(reader);
    }
}
Share:
119,251

Related videos on Youtube

disasterkid
Author by

disasterkid

Updated on July 10, 2022

Comments

  • disasterkid
    disasterkid almost 2 years

    I'm trying to convert the result I get from my web service as a string and convert it to an object.

    This is the string I'm getting from my service:

    <StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
    

    So I have a class for this as:

    [XmlRoot]
    public class StatusDocumentItem
    {
        [XmlElement]
        public string DataUrl;
        [XmlElement]
        public string LastUpdated;
        [XmlElement]
        public string Message;
        [XmlElement]
        public int State;
        [XmlElement]
        public string StateName;
    }
    

    And this is how I'm trying to get that string as an object of type StatusDocumentItem with XMLDeserializer (NB. operationXML contains the string):

    string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
    var serializer = new XmlSerializer(typeof(StatusDocumentItem));
    StatusDocumentItem result;
    
    using (TextReader reader = new StringReader(operationXML))
    {
        result = (StatusDocumentItem)serializer.Deserialize(reader);
    }
    
    Console.WriteLine(result.Message);
    

    But my result object is always empty. What am I doing wrong?

    Update. The value I get from my operationXML is like this and has an unnecessary xmlns attribute that is blocking my deserialization. Without that attribute, everything is working fine. Here is how it looks like:

    "<StatusDocumentItem xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"
    
    • Christian.K
      Christian.K over 11 years
      "operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
    • Cédric Bignon
      Cédric Bignon over 11 years
      If you set your xml example to operationXML. The deserialization works perfectly well.
    • disasterkid
      disasterkid over 11 years
      Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i=\"w3.org/2001/XMLSchema-instance\"><DataUrl/><LastUp‌​dated>2013-02-01T12:‌​13:02.0997071Z</Last‌​Updated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateNa‌​me></StatusDocumentI‌​tem>"
    • disasterkid
      disasterkid over 11 years
      @CédricBignon what do you exactly mean by setting xml example to OperationXML?
    • Cédric Bignon
      Cédric Bignon over 11 years
      @Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28‌​:13.2847259Z</LastUp‌​dated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateNa‌​me></StatusDocumentI‌​tem>";
    • disasterkid
      disasterkid over 11 years
      @CédricBignon you mean I should get rid of the xmlns attribute or should I add something to my StatusDocumentItem class?
    • Cédric Bignon
      Cédric Bignon over 11 years
      @Pedram I mean that "webRequest.getJSON(args[1], args[2], pollURL);" does not return the XML string you have written in your post.
    • disasterkid
      disasterkid over 11 years
      @CédricBignon yes it returns exactly the same string, the only difference is the xmlns attribute in the root element: xmlns:i=\"w3.org/2001/XMLSchema-instance\" the rest is the same I suppose.
    • disasterkid
      disasterkid over 11 years
      @CédricBignon oh yes you are right. So how do I get rid of that bothering attribute?
    • Cédric Bignon
      Cédric Bignon over 11 years
      @Pedram Can you edit your post by copy-paste the value of operationXML? It will help us.
    • disasterkid
      disasterkid over 11 years
      @CédricBignon affirmative. it's there now.
    • Cédric Bignon
      Cédric Bignon over 11 years
      @Pedram I have result.Message = "Job put in queue".
    • disasterkid
      disasterkid over 11 years
      @CédricBignon thank you. it's working now. however, i don't know what happened in between but you have the correct answer.
  • Pedro Martín
    Pedro Martín over 2 years
    The best way to do it, imo