We are using XMLSerializer.Deserialize(XMLReader) on .Net 4.5 to deserialize XML into an Object Graph generated by xsd.exe.
I would like to know what the expected deserialization behavior is for an array which is annotated with the XMLElementAttribute - specifically with regard to ordering. For example:
For the following property:
[System.Xml.Serialization.XmlElementAttribute("GivenName")]
public string[] GivenName {
// get() and set() methods
}
And the following XML:
<root>
<GivenName>One</GivenName>
<GivenName>Two</GivenName>
<GivenName>Three</GivenName>
</root>
Will this always deserialize as ['One', 'Two', 'Three']
So that the array order always matches the XML order
Also is there any documentation I can reference that clearly states this.
Thanks
Rob
Yes Its deserialize same order. Check the code sample code and output:
[System.SerializableAttribute()]
public class SampleClass
{
[System.Xml.Serialization.XmlElementAttribute(Order = 10)]
public string Foo { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Order = 5)]
public string Bar { get; set; }
[System.Xml.Serialization.XmlElementAttribute("GivenName", Order = 15)]
public string[] GivenNames { get; set; }
}
class Program
{
static void Main(string[] args)
{
string[] names = new string[3] { "One", "Two", "Three" };
SampleClass TestObj = new SampleClass { Bar = "dsdfsdf", Foo = "test", GivenNames = names };
XmlSerializer SerializerObj = new XmlSerializer(typeof(SampleClass));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@"C:\files\test.xml");
SerializerObj.Serialize(WriteFileStream, TestObj);
// Cleanup
WriteFileStream.Close();
// Create a new file stream for reading the XML file
FileStream ReadFileStream = new FileStream(@"C:\files\test.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
// Load the object saved above by using the Deserialize function
SampleClass LoadedObj = (SampleClass)SerializerObj.Deserialize(ReadFileStream);
// Cleanup
ReadFileStream.Close();
}
}
You can find some awesome information about it on msdn which is provide by microsoft.
This is the "official" documentation
The serialization is linear so you will alway get the same order normally.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.