mercredi 6 mai 2015

How to handle null in xml and c# -2

My First question is:

Sample code is*:

<?xml version="1.0"?>
<AddressDirectory>
  <Owner>Mayank</Owner>
  <Age>24</Age>
  <Company>BIPL</Company>
  <Address>
    <HouseNo>1</HouseNo>
    <StreetName>Pitampura</StreetName>
    <City>Delhi</City>
  </Address>
  <Address> 
  </Address>
</AddressDirectory>

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.

public class AddressDirectory
{
    public string Owner { get; set; }
    public string Age { get; set; }
    public string Company { get; set; }
    [XmlElement("Address")]
    public List<Address> addressList = new List<Address>(); 
}

public class Address
{
    public string HouseNo { get; set; }
    public string StreetName { get; set; }
    public string City { get; set; }
}

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

*: This code is taken from Mayank Gupta.

Real Situation is:

        <Address>
          <House No="1">
            <StreetName>Pitampura</StreetNamet>
            <City>Delhi</City>
        </Address>
        <Address>
        </Address>

My code is: (I'm using XmlSerializer to deserialize the xml file. The code shown below is in the class that i have created)

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

public class Address
{

     [XmlElement("House")]
     public List<House> HouseList = new List<House>();

     public class House
     {
         [XmlAttribute("No")]
         public int No{ get; set; }

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

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

The code shown above works great while serializing the xml file only if i manually delete all the null address tags. Unfortunately xml file is generated automatically so it is not possible to delete them. So i need to find a way both for null addresses and adresses with values.

Edited situation: REVISED

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