Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create model class for the xml tag. On deserializing getting the exception there is an error in reflcetion type of Model class.
My Xml is as below:
XML
<advice_tags>
  
  <aclassname>SAV1 ACCOUNT</aclassname>
  <blockedbalance>0</blockedbalance>
  <acy>
    <blocked_acy>23</blocked_acy>
    <amount_acy>56</amount_acy>
  </acy>
  
</advice_tags>

My Model class for the above Xml Tag is as below:
C#
[XmlElement("ACLASSNAME")]
       public string ACLASSNAME { get; set; }
       [XmlElement("BLOCKEDBALANCE")]
       public string BLOCKEDBALANCE { get; set; }


       [XmlArray("ACY")]
       [XmlArrayItem("BLOCKED_ACY")]
       [XmlArrayItem("AMOUNT_ACY")]
       public List<string> ACY { get; set; }


But I am getting exception on desalinizing the same for the XmlArray attribute.
C#
MDL_UBSFile UBSFile = null;
                                  XmlSerializer serializer = new XmlSerializer(typeof(MDL_UBSFile));
                                  reader = new StreamReader(Path.Combine(UBSFilePath, FileName));
                                  dss.ReadXml(reader);
                                  reader.Close();
Posted
Updated 3-Aug-16 21:42pm
v6

1 solution

Your xml become sensitive to big and small letters, so say

XML
<myclass>
   <myproperty>1</myproperty>
</myclass>


is not
C#
public class MyClass{
  public int MyProperty{ get;set;}
}


rather would be
C#
public class myclass{
  public int myproperty{ get;set;}
}


so what you need to do is match the way you wrote it

... (updating solution) ...
So if that doesn't work, i suggest going the other way about. Make the class first

and it is clear by observing your xml that a class AdviceTag could be generated with two simply types and one complex type called Acy with two values (Blocked and amount), perhaps like this:

C#
public class AdviceTag
    {
        public string AClassName { get; set; }
        public string BlockedBalance { get; set; }

        public Acy Acy { get; set; }
    }

    public class Acy
    {
        public string Blocked{ get; set; }
        public string Amount { get; set; }
    }


so that in place let's simply generate the xml by instantiating an instance of that class and serialize it

C#
var aTag = new AdviceTag { AClassName = "Sav1 Account", BlockedBalance = "0", Acy = new Acy { Amount = "56", Blocked = "23" } };

           var ser = new XmlSerializer(typeof(AdviceTag));

           string filename = Assembly.GetExecutingAssembly().Location;
           int pos = filename.LastIndexOf(@"\");
           filename = filename.Substring(0, pos +1) + "myfile.xml";
           using(var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
           {
               ser.Serialize(fs, aTag);
               fs.Flush();
               fs.Close();
           }


We're placing the serialized xml in same location as executing assembly and calling it myfile.xml, it then looks like this:
XML
<?xml version="1.0"?>
<AdviceTag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <AClassName>Sav1 Account</AClassName>
  <BlockedBalance>0</BlockedBalance>
  <Acy>
    <Blocked>23</Blocked>
    <Amount>56</Amount>
  </Acy>
</AdviceTag>


And now what was the mistake?

Well your xml designates a complex type called acy containing two member properties, but you were struggleing to put them in an arraylist, where you don't have to.
 
Share this answer
 
v2
Comments
Ema112 9-Aug-16 3:41am    
I chnaged so. But still the same error mentioned above comes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900