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>
<DefaulityVisib ility>On</DefaulityVisibi lity>
</layer>
<layer>
<trackNumber> 2</trackNumber>
<name>test2</name>
<DefaulityVisib ility>On</DefaulityVisibi lity>
</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("L ayerCatalog.xml ");
XmlSerializer serializer = new XmlSerializer(t ypeof(layercata log));
tocLayerCatalog = (layercatalog)s erializer.Deser ialize(reader);
reader.Close();
//loop each catetory
IEnumerator categoryList = tocLayerCatalog .Items.GetEnume rator();
while (categoryList.M oveNext())
{
layercatalogCat egory catetory= (layercatalogCa tegory)category List.Current;
string jcode = catetory.jcode;
//loop each layer within the category
if (catetory.layer List == null) continue;
layercatalogCat egoryLayerListL ayer[][] layerlistArray = catetory.layerL ist;
for (int i = 0; i < layerlistArray. Length; i++)
{
//Attempt to get the content of the layer tag but no luck
layercatalogCat egoryLayerListL ayer[] l = (layercatalogCa tegoryLayerList Layer[])layerlistArray[i];
IEnumerator layer = l.GetEnumerator ();
while (layer.MoveNext ())
{
layercatalogCat egoryLayerListL ayer layeritem = (layercatalogCa tegoryLayerList Layer)layer.Cur rent;
string name = layeritem.name;
}
}
}
}catch(XmlExcep tion xe){
string x= xe.Message + "XML Parse Error";
}catch(InvalidO perationExcepti on ioe){
string y = ioe.InnerExcept ion.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
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>
<DefaulityVisib ility>On</DefaulityVisibi lity>
</layer>
<layer>
<trackNumber> 2</trackNumber>
<name>test2</name>
<DefaulityVisib ility>On</DefaulityVisibi lity>
</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("L ayerCatalog.xml ");
XmlSerializer serializer = new XmlSerializer(t ypeof(layercata log));
tocLayerCatalog = (layercatalog)s erializer.Deser ialize(reader);
reader.Close();
//loop each catetory
IEnumerator categoryList = tocLayerCatalog .Items.GetEnume rator();
while (categoryList.M oveNext())
{
layercatalogCat egory catetory= (layercatalogCa tegory)category List.Current;
string jcode = catetory.jcode;
//loop each layer within the category
if (catetory.layer List == null) continue;
layercatalogCat egoryLayerListL ayer[][] layerlistArray = catetory.layerL ist;
for (int i = 0; i < layerlistArray. Length; i++)
{
//Attempt to get the content of the layer tag but no luck
layercatalogCat egoryLayerListL ayer[] l = (layercatalogCa tegoryLayerList Layer[])layerlistArray[i];
IEnumerator layer = l.GetEnumerator ();
while (layer.MoveNext ())
{
layercatalogCat egoryLayerListL ayer layeritem = (layercatalogCa tegoryLayerList Layer)layer.Cur rent;
string name = layeritem.name;
}
}
}
}catch(XmlExcep tion xe){
string x= xe.Message + "XML Parse Error";
}catch(InvalidO perationExcepti on ioe){
string y = ioe.InnerExcept ion.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