I have a class. I want to deserialize XML file into this class.
public partial class Form1 : Form
{
public string xmlFile;
public Form1()
{
InitializeComponent();
xmlFile = "Sample.xml";
}
[Serializable]
public class Application
{
public string AppName;
public string UpdateDetail;
};
[Serializable]
public class Applications
{
[XmlElement]
public Application[] Application;
};
[Serializable]
public class Pmsp_Update
{
public string OldVersion;
public string NewVersion;
public Applications Applications;
};
private void btnRead_Click(object sender, EventArgs e)
{
XmlReader reader = XmlReader.Create(xmlFile);
XmlSerializer ser = new XmlSerializer(typeof(Pmsp_Update));
Pmsp_Update pu;
using (reader = XmlReader.Create(xmlFile))
{
pu = (Pmsp_Update)ser.Deserialize(reader);
}
}
}
And here is the XML:
<Pmsp_Update>
<OldVersion>v4.0.0</OldVersion>
<NewVersion>v4.0.1</NewVersion>
<Applications>
<Application>
<AppName>SampleApp</AppName>
<UpdateDetail>sample</UpdateDetail>
</Application>
</Applications>
</Pmsp_Update>
I want to ask about attributes. I only used [Serializable] attribute and I can get the class. I don't use any [XmlElement] attribute and this didn't cause any error except one.
If I use like this I can populate the array but:
[Serializable]
public class Applications
{
[XmlElement]
public Application[] Application;
};
If I use like this I can't populate the array:
[Serializable]
public class Applications
{
public Application[] Application;
};
So, my question is this: I am not using [XmlElement] for fields and everything works well but I can't populate the array. Why couldn't I populate the array when I don't use [XmlElement] although I can populate the fields?
[Serializable]has nothing to do withXmlSerializer. It enables binary serialization.