Hello,
I am trying to use XmlSerializer to deserialize an xml file and then use a loop to access the content. With the help of xsd.exe, a serializer class is generated and I am able to use that to deserialize the following xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<layercatalog>
<category jcode="aaa">
<layerList>
<layer>
<trackNumber>1</trackNumber>
<name>test1</name>
<DefaulityVisibility>On</DefaulityVisibility>
</layer>
<layer>
<trackNumber>2</trackNumber>
<name>test2</name>
<DefaulityVisibility>On</DefaulityVisibility>
</layer>
</layerList>
</category>
<category jcode="bbb" />
<category jcode="ccc" />
<category jcode="ddd" />
<category jcode="eee" />
<category jcode="fff" />
<category jcode="ggg" />
<category jcode="hhh" />
<category jcode="iii" />
</layercatalog>
The problem is I cannot seems to get the <layer> details, I not not sure if there is something wrong with my code below:
try{
TextReader reader = new StreamReader("LayerCatalog.xml");
XmlSerializer serializer = new XmlSerializer(typeof(layercatalog));
tocLayerCatalog = (layercatalog)serializer.Deserialize(reader);
reader.Close();
//loop each catetory
IEnumerator categoryList = tocLayerCatalog.Items.GetEnumerator();
while (categoryList.MoveNext())
{
layercatalogCategory catetory= (layercatalogCategory)categoryList.Current;
string jcode = catetory.jcode;
//loop each layer within the category
if (catetory.layerList == null) continue;
layercatalogCategoryLayerListLayer[][] layerlistArray = catetory.layerList;
for (int i = 0; i < layerlistArray.Length; i++)
{
//Attempt to get the content of the layer tag but no luck
layercatalogCategoryLayerListLayer[] l = (layercatalogCategoryLayerListLayer[])layerlistArray[i];
IEnumerator layer = l.GetEnumerator();
while (layer.MoveNext())
{
layercatalogCategoryLayerListLayer layeritem = (layercatalogCategoryLayerListLayer)layer.Current;
string name = layeritem.name;
}
}
}
}catch(XmlException xe){
string x= xe.Message + "XML Parse Error";
}catch(InvalidOperationException ioe){
string y = ioe.InnerException.Message + "XML Serialization Error";
}
Can someone shed some light on me, I have been stucked with this problem for awhile. Please me know if you need to cs class and I will email it to you separately.
Thanks