Jay

Jay

  • NA
  • 48
  • 0

Help with XML Serialization in C#

Dec 3 2008 9:49 AM

Hi Everyone,

I was wondering if someone could help me with this.  I am sending requests and getting responses from a server using XML serialization.  I have to make an XML request that looks like this.....

<EntityRequest>
       <Entity>
               <EntityName>My Name</EntityName>
               <Address>
                       <Street>My Street</Street>
                       <City>My City</City>
                       <State>My State</State>
                       <PostalCode>12345</PostalCode>
               </Address>
               <ContactList>
                        <Contacts>
                                <Description>FAX</Description>
                                 <Telephone>
                                         <Number>123-456-7890</Number>
                                  </Telephone>
                          </Contacts>
                          <Contacts>
                                  <Description>HOME</Description>
                                   <Telephone>
                                           <Number>321-654-9870</Number>
                                   </Telephone>
                           </Contacts>
                           <Contacts>
                                   <Description>CELL</Description>
                                    <Telephone>
                                             <Number>500-894-4569</Number>
                                     </Telephone>
                            </Contacts>
                   </ContactList>
           </Entity>
</EntityRequest>

I need to populate this request with information to send to the server.  Now, I already have the seialization and deserialization already programmed and working.  My issue is that the <Contacts> tags need to be put in some kind of array.  This wouldn't be that bad but some of the tags are nested deeper like the <Number> tags.  I have the Contacts part in a class and the Telephone part as another class instantiated inside the Contacts class. Bottom line is I think I need to insert the group of Contact tags into an array to have a multiple group of Contact tags as needed for more than 1 contact.

My c# code looks like.......

EntityRequest request = new EntityRequest();

request.entityRequest.entity.entityName = "My Name";

request.entityRequest.entity.address.streetLineOne = "My Street";

request.entityRequest.entity.address.city = "My City";

request.entityRequest.entity.address.state = "My State";

request.entityRequest.entity.address.postalCode = "12345";

//The above code works fine, but when I try to use the array..

request.entityRequest.entity.contactList.contacts[0].description = "FAX";

request.entityRequest.entity.contactList.contacts[0].telephone.number = "123-456-7890";

 I get the error "Object reference not set to an instance of an object" pointed at the array.

Could anyone help me put this in a proper array?  I will provide more code and explainations if needed.  Thanks to everyone in advance.

Regards,

Jay


Answers (1)