Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem while Deserializing [Solved]

Problem while Deserializing [Solved]

Scheduled Pinned Locked Moved C#
helpxmljson
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nagendrathecoder
    wrote on last edited by
    #1

    hi all, i am trying to deserialize a xml string into an object but i am getting following error : "An exception of type 'System.InvalidCastOperationException' occured in System.Xml.dll but was not handled in user code. Additional information: There is an error in XML document(1,2)." I am using following code to deserialize :

    object obj = new object();
    System.Xml.Serialization.XmlSerializer x = new XmlSerializer(obj.GetType());
    StringReader sr = new StringReader(strNewXml);// strNewXml is a string in xml format
    XmlTextReader xml = new XmlTextReader(sr);
    obj = (object)x.Deserialize(xml);//Here i am getting error

    I tried to see the value of XmlTextReader xml, but it is showing as {None}. I even tried with a string which i earlier serialized from an object but same error is displaying with it too. Can anyone tell me where i am doing wrong. Thanks and Regards, Nagendra

    N 1 Reply Last reply
    0
    • N nagendrathecoder

      hi all, i am trying to deserialize a xml string into an object but i am getting following error : "An exception of type 'System.InvalidCastOperationException' occured in System.Xml.dll but was not handled in user code. Additional information: There is an error in XML document(1,2)." I am using following code to deserialize :

      object obj = new object();
      System.Xml.Serialization.XmlSerializer x = new XmlSerializer(obj.GetType());
      StringReader sr = new StringReader(strNewXml);// strNewXml is a string in xml format
      XmlTextReader xml = new XmlTextReader(sr);
      obj = (object)x.Deserialize(xml);//Here i am getting error

      I tried to see the value of XmlTextReader xml, but it is showing as {None}. I even tried with a string which i earlier serialized from an object but same error is displaying with it too. Can anyone tell me where i am doing wrong. Thanks and Regards, Nagendra

      N Offline
      N Offline
      nagendrathecoder
      wrote on last edited by
      #2

      I have solved the problem by modifying my code as below:

      object objNewTransactionEntry = new object();
      XmlRootAttribute xmlRoot = new XmlRootAttribute("Session");
      System.Xml.Serialization.XmlSerializer y = new XmlSerializer(objNewTransactionEntry.GetType(), xmlRoot);
      StringReader sReader = new StringReader(strNewXml);
      XmlTextReader xmlReader = new XmlTextReader(sReader);
      objNewTransactionEntry = (object)y.Deserialize(xmlReader);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups