I'm using silverlight and doing serialisation using c# code.
I have a parameters
class and inside it I have parameter
and separator
lists:
[XmlRoot(ElementName = "parameters")]
public class Parameters
{
[XmlElement("separator")]
public List<Separator> Separator { get { return a1; } }
private readonly List<Separator> a1 = new List<Separator>();
[XmlElement("parameter")]
public List<Parameter> Parameter { get { return b1; } }
private readonly List<Parameter> b1 = new List<Parameter>();
}
And again inside the Parameter class I have:
[XmlElement("name")]
public string Name { get; set; }
And also:
[XmlElement("component")]
public List<Component> Component { get { return b3; } }
private readonly List<Component> b3 = new List<Component>();
and so on.
Now I'm using XmlWriter below in the main function:
public void Main()
{
Parameters[] prs = new Parameters[4];
prs[0] = new Parameters();
prs[1] = new Parameters();
prs[2] = new Parameters();
prs[0].Parameter[0].Name = "shek1";
prs[0].Parameter[0].Component[0].Type = "type1";
prs[1].Separator[0].Separators = "sep1";
prs[2].Parameter[0].Name = "shek2";
prs[2].Parameter[0].Component[0].Type = "type2";
StringBuilder output = new StringBuilder();
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
writer.WriteStartDocument();
writer.WriteStartElement("Parameters");
int count = 0;
foreach (var param in prs)
{
writer.WriteStartElement("Parameter");
writer.WriteElementString("Name", param.Parameter[count].Name);
writer.WriteStartElement("Component");
writer.WriteElementString("Type", param.Parameter[count].Component[0].Type);
writer.WriteStartElement("Attribute");
writer.WriteElementString("Items", param.Parameter[count].Component[0].Attributes[0].Items[count]);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
The error I'm getting is Index was out of range. It must not be negative and must be less than the size of the collection.
in line prs[0].Parameter[0].Name = "shek1";
.
I know the problem is inside the Parameters class. I have the Parameter class and it is working if I assign any value to the field of Parameters class, but when I go to the Parameter class then it has this index out of range error.
How do I solve this problem?