vendredi 8 mai 2015

How to handle null in xml and c# -2

Let's consider that this was automatically generated xml file and in the second Address tag there is nothing. It is null.

If it was not null , we are going to make a list for address and then continue with a class declaration as below. But if we do so, because of a null address tag it gives us an error.

If possible I would like to add an if statement saying that

if it is null , store it as string. if it is not null , go with the list thing.

Could you please show me in code part how to do that?

Best regards

xml sample:

<?xml version="1.0"?>
<XMLCD>
 <Personnel>
  <AddressDirectory>
    <Owner>Jerry</Owner>
    <Age>29</Age>
    <Company>123</Company>
    <Address>
     <ST>
      <HouseNo>1</HouseNo>
      <StreetName>5th</StreetName>
      <Town>Elmsford</Town>
     </ST>
    </Address>
    <Address> 
    </Address>
  </AddressDirectory>
  <AddressDirectory>
      <Owner>Joe</Owner>
      <Age>24</Age>
      <Company>456</Company>
      <Address>
       <ST>
        <HouseNo>1</HouseNo>
        <StreetName>10Blvd</StreetName>
        <Town>StMichael</Town>
       </ST>
      </Address>
      <Address> 
      </Address>
  </AddressDirectory>
 </Personnel>
</XMLCD>

My code:

        OpenFileDialog ofd = new OpenFileDialog();
        opnFileName = ofd.FileName;

        XmlSerializer deserializer = new XmlSerializer(typeof(XMLCD));
        TextReader reader = new StreamReader(this.opnFileName);
        object obj = deserializer.Deserialize(reader);
        XMLCD XmlData = (XMLCD)obj;
        reader.Close();

    public class XMLCD
    {
         [XmlElement("Personnel")]
         public List<Personnel> PersonnelList = new List<Personnel>();

         public class Personnel
         {
           [XmlElement("AddressDirectory")]
           public List<AddressDirectory> AddressDirectoryList = new List<AddressDirectory>();

           public class AddressDirectory
           {
                 [XmlElement("Owner")]
                 public string Owner{ get; set; }

                 [XmlElement("Age")]
                 public string Age{ get; set; }

                 [XmlElement("Company")]
                 public string Company{ get; set; }


                 [XmlElement("Address")]
                 public List<Address> AddressList = new List<Address>();

                 public class Address
                 {
                       [XmlElement("ST")]
                       public List<ST> STList = new List<ST>();

                       public class ST
                       {
                         [XmlElement("HouseNo")]
                         public string HouseNo{ get; set; }

                         [XmlElement("StreetName")]
                         public string StreetName{ get; set; }

                         [XmlElement("Town")]
                         public string Town{ get; set; }
                }
     }
  }
}

The code above works great only for xml codes who have no null adress tags. When the xml code has null adress tags as shown above it gives an error.

Aucun commentaire:

Enregistrer un commentaire