I get
InvalidCastException: value is not a convertible object: System.String to IdTag
while trying to deserialize the xml attribute.
Here is the xml sample:
<?xml version="1.0" encoding="windows-1250"?> <ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Item Name="Item Name" ParentId="SampleId" /> </ArrayOfItem>
Class examples:
public class Item { [XmlAttribute] public string Name { get; set; } [XmlAttribute] public IdTag ParentId { get; set; } } [Serializable] public class IdTag { public string id; }
The exception is thrown from the Convert.ToType() method (which is called from the XmlSerializer ). AFAIK there is no way to "implement" an IConvertible interface for System.String to convert to IdTag . I know that I can implement the proxy property ie:
public class Item { [XmlAttribute] public string Name {get; set;} [XmlAttribute("ParentId")] public string _ParentId { get; set; } [XmlIgnore] public IdTag ParentId { get { return new IdTag(_ParentId); } set { _ParentId = value.id; } } }
Is there another way?
c # xml deserialization attributes xmlserializer
Chris roskinski
source share