If my XML is like this:
<Item>
<Name>Jerry</Name>
<Array>
<Item>
<Name>Joe</Name>
</Item>
<Item>
<Name>Sam</Name>
</Item>
</Array>
</Item>
I can serialize it into this class:
[DataContract(Namespace = "", Name = "dict")]
public class Item
{
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Array")]
public IEnumerable<Item> Children { get; set; }
}
But what if my XML is like this?
<Item>
<Name>Jerry</Name>
<Item>
<Name>Joe</Name>
</Item>
<Item>
<Name>Sam</Name>
</Item>
</Item>
This does not work:
[DataContract(Namespace = "", Name = "Item")]
public class Item
{
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Item")]
public IEnumerable<Item> Children { get; set; }
}
What is the right way to decorate the class?
To deserialize the objects, call the Deserialize method with the FileStream as an argument. The deserialized object must be cast to an object variable of type PurchaseOrder . The code then reads the values of the deserialized PurchaseOrder .
Xml. Serialization namespace) class is used to serialize and deserialize. The class method Serialize is called. Since we have to serialize in a file, we create a " TextWriter ".
Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.
Since XmlSerializer is one of the few thread safe classes in the framework you really only need a single instance of each serializer even in a multithreaded application.
If you want control over xml, then XmlSerializer
is the way to go, not DataContractSerializer
. The latter simply lacks the fine-grained control that XmlSerializer
offers; in this case for things like list/array layout - but even just xml attributes are a problem for DataContractSerializer
. Then it is just:
public class Item {
public string Name { get; set; }
[XmlElement("Item")]
public List<Item> Children { get; set; }
}
public class Item {
public string Name { get; set; }
}
and:
var serializer = new XmlSerializer(typeof(Item));
var item = (Item) serializer.Deserialize(source);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With