Deserializing XML from String












26















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>"









share|improve this question




















  • 1





    "operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.

    – Christian.K
    Feb 1 '13 at 12:10






  • 1





    If you set your xml example to operationXML. The deserialization works perfectly well.

    – Cédric Bignon
    Feb 1 '13 at 12:12













  • Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"

    – disasterkid
    Feb 1 '13 at 12:14






  • 1





    @Pedram string operationXML = "<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>";

    – Cédric Bignon
    Feb 1 '13 at 12:16






  • 1





    @Pedram I have result.Message = "Job put in queue".

    – Cédric Bignon
    Feb 1 '13 at 12:43
















26















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>"









share|improve this question




















  • 1





    "operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.

    – Christian.K
    Feb 1 '13 at 12:10






  • 1





    If you set your xml example to operationXML. The deserialization works perfectly well.

    – Cédric Bignon
    Feb 1 '13 at 12:12













  • Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"

    – disasterkid
    Feb 1 '13 at 12:14






  • 1





    @Pedram string operationXML = "<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>";

    – Cédric Bignon
    Feb 1 '13 at 12:16






  • 1





    @Pedram I have result.Message = "Job put in queue".

    – Cédric Bignon
    Feb 1 '13 at 12:43














26












26








26


7






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>"









share|improve this question
















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>"






c# .net xml xml-deserialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 1 '13 at 12:36







disasterkid

















asked Feb 1 '13 at 12:08









disasterkiddisasterkid

2,3781453106




2,3781453106








  • 1





    "operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.

    – Christian.K
    Feb 1 '13 at 12:10






  • 1





    If you set your xml example to operationXML. The deserialization works perfectly well.

    – Cédric Bignon
    Feb 1 '13 at 12:12













  • Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"

    – disasterkid
    Feb 1 '13 at 12:14






  • 1





    @Pedram string operationXML = "<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>";

    – Cédric Bignon
    Feb 1 '13 at 12:16






  • 1





    @Pedram I have result.Message = "Job put in queue".

    – Cédric Bignon
    Feb 1 '13 at 12:43














  • 1





    "operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.

    – Christian.K
    Feb 1 '13 at 12:10






  • 1





    If you set your xml example to operationXML. The deserialization works perfectly well.

    – Cédric Bignon
    Feb 1 '13 at 12:12













  • Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"

    – disasterkid
    Feb 1 '13 at 12:14






  • 1





    @Pedram string operationXML = "<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>";

    – Cédric Bignon
    Feb 1 '13 at 12:16






  • 1





    @Pedram I have result.Message = "Job put in queue".

    – Cédric Bignon
    Feb 1 '13 at 12:43








1




1





"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.

– Christian.K
Feb 1 '13 at 12:10





"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.

– Christian.K
Feb 1 '13 at 12:10




1




1





If you set your xml example to operationXML. The deserialization works perfectly well.

– Cédric Bignon
Feb 1 '13 at 12:12







If you set your xml example to operationXML. The deserialization works perfectly well.

– Cédric Bignon
Feb 1 '13 at 12:12















Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"

– disasterkid
Feb 1 '13 at 12:14





Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"

– disasterkid
Feb 1 '13 at 12:14




1




1





@Pedram string operationXML = "<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>";

– Cédric Bignon
Feb 1 '13 at 12:16





@Pedram string operationXML = "<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>";

– Cédric Bignon
Feb 1 '13 at 12:16




1




1





@Pedram I have result.Message = "Job put in queue".

– Cédric Bignon
Feb 1 '13 at 12:43





@Pedram I have result.Message = "Job put in queue".

– Cédric Bignon
Feb 1 '13 at 12:43












2 Answers
2






active

oldest

votes


















65














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"?






share|improve this answer































    -1














                AccountRequest xml = null;
    var serializer = new XmlSerializer(typeof(AccountRequest));
    using (TextReader reader = new StringReader(requestXml))
    {
    xml = (AccountRequest)serializer.Deserialize(reader);
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14645964%2fdeserializing-xml-from-string%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      65














      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"?






      share|improve this answer




























        65














        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"?






        share|improve this answer


























          65












          65








          65







          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"?






          share|improve this answer













          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"?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 '13 at 12:45









          Cédric BignonCédric Bignon

          10.5k13248




          10.5k13248

























              -1














                          AccountRequest xml = null;
              var serializer = new XmlSerializer(typeof(AccountRequest));
              using (TextReader reader = new StringReader(requestXml))
              {
              xml = (AccountRequest)serializer.Deserialize(reader);
              }





              share|improve this answer




























                -1














                            AccountRequest xml = null;
                var serializer = new XmlSerializer(typeof(AccountRequest));
                using (TextReader reader = new StringReader(requestXml))
                {
                xml = (AccountRequest)serializer.Deserialize(reader);
                }





                share|improve this answer


























                  -1












                  -1








                  -1







                              AccountRequest xml = null;
                  var serializer = new XmlSerializer(typeof(AccountRequest));
                  using (TextReader reader = new StringReader(requestXml))
                  {
                  xml = (AccountRequest)serializer.Deserialize(reader);
                  }





                  share|improve this answer













                              AccountRequest xml = null;
                  var serializer = new XmlSerializer(typeof(AccountRequest));
                  using (TextReader reader = new StringReader(requestXml))
                  {
                  xml = (AccountRequest)serializer.Deserialize(reader);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 7:42









                  Varun Tej Reddy PutchakayalaVarun Tej Reddy Putchakayala

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14645964%2fdeserializing-xml-from-string%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy