Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

XML:

<?xml version="1.0" encoding="utf-8" ?>

<TTTTModel>

<TTTTClass>

<TTTTType>Deployment</TTTTType>

<TTTTDate>2009-09-30T00:00:00-07:00</TTTTDate>

</TTTTClass>

<TTTTClass>

<TTTTType>Deployment</TTTTType>

<TTTTDate>2009-09-20T00:00:00-07:00</TTTTDate>

</TTTTClass>

<TTTTClass>

<TTTTType>Copy</TTTTType>

<TTTTDate>2009-02-23T00:00:00-07:00</TTTTDate>

</TTTTClass>

</TTTTModel>

 

 

Code:

 

[serializable]
   public class ActionClass
   {
       [XmlElement(Form=XmlSchemaForm.Unqualified)]
       public string ActionType { get; set; }
       [XmlElement(Form = XmlSchemaForm.Unqualified)]
       public DateTime ActionDate { get; set; }
   }




   public class ActionModel : List<ActionClass>
   {
       public ActionModel()
       {
           XmlSerializer xs = new XmlSerializer(typeof(ActionModel), "");

           using (Stream ms = new FileStream("Demo.xml", FileMode.Open, FileAccess.Read))
           {
               object inp = xs.Deserialize(ms);
               this.AddRange((ActionModel)inp);
           }
       }
   }

 

Why I get exception on object inp = xs.Deserialize(ms)?

 

<ActionModel xmlns=''> was not expected.

  • Administrators
Posted

The element names in the actual XML file are not what the serializer is expecting, by default it assumes the element names are the same as the property names.

 

Try adding the relevant attributes to the class i.e.

[XmlRoot(ElementName = "TTTTClass")]
   public class ActionClass
   {
       [XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "TTTTType")]
       public string ActionType { get; set; }
       [XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "TTTTDate")]
       public DateTime ActionDate { get; set; }
   }



   [XmlRoot(ElementName = "TTTTModel")]
   public class ActionModel : List
   {
       public ActionModel()
       {
           XmlSerializer xs = new XmlSerializer(typeof(ActionModel), "");

           using (Stream ms = new FileStream("Demo.xml", FileMode.Open, FileAccess.Read))
           {
               object inp = xs.Deserialize(ms);
               this.AddRange((ActionModel)inp);
           }
       }
   }

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...