senglory Posted November 21, 2009 Posted November 21, 2009 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. Quote
Administrators PlausiblyDamp Posted November 21, 2009 Administrators Posted November 21, 2009 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); } } } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.