I have defined the following classes.
Document.cs
public class Document {
// ...
[XmlAttribute]
public string Status { get; set; }
}
DocumentOrder.cs
public class DocumentOrder {
// ...
[XmlAttribute]
public string Name { get; set; }
public List<Document> Documents { get; set; }
}
When serializing this to an XML I get:
<DocumentOrder Name="myname">
<Documents>
<Document Status="new"/>
// ...
</Documents>
</DocumentOrder>
But I would like to have it like that, i.e. be the Document
elements to be children of DocumentOrder
.
<DocumentOrder Name="myname">
<Document Status="new"/>
<Document Status="new"/>
<Document Status="new"/>
// The document element has other attributes to distinguish...
</DocumentOrder>
How can I do that?
Use the BinaryFormatter class. List, serialize. In C# programs we often need to read and write data from the disk. A List can be serialized—here we serialize (to a file) a List of objects.
Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.
As with the CreatePo method, you must first construct an XmlSerializer, passing the type of the class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.
you can try :
public class DocumentOrder {
// ...
[XmlAttribute]
public string Name { get; set; }
[XmlElement("Document")]
public List<Document> Documents { get; set; }
}
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