Deserializations of xml file - IXmlSerializable

Hi, I have a xml file as below :
So far i am able to do everything but i cannot deserialize the orderline element.
I read some notes  and it directs to use IXmlSerializable  for the class.
Even after that i am quite confised.
How do i deserialise Order  so that it can have multiple orderline elements.

*************************************
<biztalk_1>
      
      <body>
            <Order >
                  
                  </OrderLine>
                               <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCodexxxxx</BuyersProductCode>
                              <Description>xxxx1 customer Part List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
</OrderLine>
                               <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCodexxxxx</BuyersProductCode>
                              <Description>xxxx1 customer Part List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
*************************************************************************************************************
public class biztalk_1
    {
        public body body;
    }
    public class body
    {
        [XmlElement("Order")]
        public Order Order;
    }
    public class Order
    {
         public List<orderLine> OrderLine;
    }
         
    public class OrderLine
    {
        private int _LineNumber;
        private Product _Product;
        private Quantity _Quantity;
        
        public int LineNumber
        {
            set { _LineNumber = value; }
            get { return _LineNumber; }
        }
        public Product Product
        {
            set { _Product = value; }
            get { return _Product; }
        }
        public Quantity Quantity
        {
            set { _Quantity = value; }
            get { return _Quantity; }
        }  
    }
    public class Product
    {
        private string _BuyersProductCode;
        private string _Description;
 
        public string BuyersProductCode
        {
            set { _BuyersProductCode = value; }
            get { return _BuyersProductCode; }
        }
        public string Description
        {
            set { _Description = value; }
            get { return _Description; }
        }
    }
    public class Quantity
    {
        private int _Amount;
        public int Amount
        {
            set { _Amount = value; }
            get { return _Amount; }
        }
    }

Open in new window

WannabeNerdAsked:
Who is Participating?
 
WannabeNerdConnect With a Mentor Author Commented:
This is how it is done :
*************************
public class Order
    {
        public OrderReferences OrderReferences;
        private string _OrderDate;
        public Supplier Supplier;
        public Buyer Buyer;
        public Delivery Delivery;
        public InvoiceTo InvoiceTo;



        /// This is very important part
       
         private ArrayList ListOrderLine;

        public Order()
        {
            ListOrderLine = new ArrayList();
        }
        [XmlElement("OrderLine")]
        public OrderLine[] OrderLineArray
        {
            get
            {
                OrderLine[] OrderLineArray = new OrderLine[ListOrderLine.Count];
                ListOrderLine.CopyTo(OrderLineArray);
                return OrderLineArray;
            }
            set
            {
                if (value == null) return;
                OrderLine[] OrderLineArray = (OrderLine[])value;
                ListOrderLine.Clear();
                foreach (OrderLine orderline in OrderLineArray)
                    ListOrderLine.Add(orderline);
            }
        }
        public int AddOrderLine(OrderLine orderline)
        { return ListOrderLine.Add(orderline); }

        /// </summary>

                         
        public string OrderDate
        {
            set { _OrderDate = value; }
            get { return _OrderDate; }
        }
    }
0
 
iHadiCommented:
Hi

you have a error in your Xml file syntax. you do not have an opening OrderLine tag. your are starting immediately with the closing tag </OrderLine>.
0
 
WannabeNerdAuthor Commented:
Oh sorry  my mistake. Actually i am more looking for concept then code.
If anyone will give me concept how i will achieve this that will be great.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
WannabeNerdAuthor Commented:
    <Order >
                 
                  <OrderLine>
                               <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCodexxxxx</BuyersProductCode>
                              <Description>xxxx1 customer Part List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                               <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCodexxxxx</BuyersProductCode>
                              <Description>xxxx1 customer Part List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
</Order>
0
 
Anurag ThakurTechnical ManagerCommented:
your serialized xml file should be in the same format as the object you are going to deserialize it into
read the folloing articles might help you in understanding the concept a little better
http://geekswithblogs.net/TimH/archive/2006/02/09/68857.aspx
http://sharpertutorials.com/serialization/
0
 
WannabeNerdAuthor Commented:
Yea the concep is clear about it but as you can see that i have more then one OrderLine Object.
and order line is a class. How do i deserialize it ?

do i have to create array of class or something like it ?
0
 
saraganiCommented:
Well, your XML contains some problems, like <BuyersProductCodexxxxx
while it should be <BuyersProductCode>xxxxx

Beside that, your XML design shows that Order is the list of the OrderLines, so instead of having a class called order which contain a list, make Order to be the list.


The XML file:

<biztalk_1>
     
      <body>
     <Order >
                 
                  <OrderLine>
                               <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxxx</BuyersProductCode>
                              <Description>xxxx1 customer Part List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                               <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxxx</BuyersProductCode>
                              <Description>xxxx1 customer Part List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
      </Order>  
      </body>
</biztalk_1>



The Class structure:


    public class biztalk_1
    {
        public body body;
    }
    public class body
    {
        public List<OrderLine> Order;
    }

    public class OrderLine
    {
        private int _LineNumber;
        private Product _Product;
        private Quantity _Quantity;

        public int LineNumber
        {
            set { _LineNumber = value; }
            get { return _LineNumber; }
        }
        public Product Product
        {
            set { _Product = value; }
            get { return _Product; }
        }
        public Quantity Quantity
        {
            set { _Quantity = value; }
            get { return _Quantity; }
        }
    }
    public class Product
    {
        private string _BuyersProductCode;
        private string _Description;

        public string BuyersProductCode
        {
            set { _BuyersProductCode = value; }
            get { return _BuyersProductCode; }
        }
        public string Description
        {
            set { _Description = value; }
            get { return _Description; }
        }
    }
    public class Quantity
    {
        private int _Amount;
        public int Amount
        {
            set { _Amount = value; }
            get { return _Amount; }
        }
    }




And the code for the deserialization:

            XmlSerializer xs = new XmlSerializer(typeof(biztalk_1));
            biztalk_1 bt;
            using (FileStream fs = new FileStream(@"c:\1.xml",FileMode.Open))
            {
                bt = (biztalk_1)xs.Deserialize(fs);
            }


This way you don't even need to remember to close the FileStream
0
 
WannabeNerdAuthor Commented:
Yea i tried it but if you'll see that i have mroe elements in order elemtent. What i am going to do with that ?
Actual XML :-
*********************


<biztalk_1 xmlns="urn:schemas-biztalk-org:biztalk:biztalk_1">
      <header>
            <delivery>
                  <message/>
                  <to>
                        <address/>
                  </to>
                  <from>
                        <address/>
                  </from>
            </delivery>
            <manifest>
                  <document>
                        <name>Order</name>
                        <description>EBis Order</description>
                  </document>
            </manifest>
      </header>
      <body>
            <Order xmlns="urn:www.basda.org/schema/eBIS-XML_order_v3.00.xml">
                  <OrderHead>
                        <Schema>
                              <Version>3.00</Version>
                        </Schema>
                        <Stylesheet>
                              <StylesheetOwner/>
                              <StylesheetName>eBIS_MAM.xsl</StylesheetName>
                              <Version/>
                              <StylesheetType>xsl</StylesheetType>
                        </Stylesheet>
                        <Parameters>
                              <Language>en_GB</Language>
                              <DecimalSeparator>.</DecimalSeparator>
                              <Precision>20.2</Precision>
                        </Parameters>
                        <OriginatingSoftware>
                              <SoftwareManufacturer>MAM</SoftwareManufacturer>
                              <SoftwareProduct>Autopart</SoftwareProduct>
                              <SoftwareVersion>17 YL</SoftwareVersion>
                        </OriginatingSoftware>
                        <OrderType Code="PUO"/>
                        <Function Code="FIO"/>
                        <OrderCurrency>
                              <Currency Code="GBP"/>
                        </OrderCurrency>
                        <Checksum>36862</Checksum>
                  </OrderHead>
                  <OrderReferences>
                        <BuyersOrderNumber>AP115502</BuyersOrderNumber>
                  </OrderReferences>
                  <OrderDate>2008-12-04T13:35</OrderDate>
                  <Supplier>
                        <SupplierReferences>
                              <BuyersCodeForSupplier>xxx</BuyersCodeForSupplier>
                        </SupplierReferences>
                        <Party>xxx</Party>
                        <Address>
                              <AddressLine>xxxL</AddressLine>
                              <AddressLine>xxx.</AddressLine>
                              <AddressLine>xxx</AddressLine>
                              <AddressLine>xxx</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>xxx</AddressLine>
                        </Address>
                        <Contact>
                              <Name>-</Name>
                              <Department>Sales</Department>
                              <DDI>xxxxx</DDI>
                        </Contact>
                  </Supplier>
                  <Buyer>
                        <BuyerReferences>
                              <SuppliersCodeForBuyer>xxxx</SuppliersCodeForBuyer>
                              <GLN>3UE372961233NR3</GLN>
                        </BuyerReferences>
                        <Party>EU LTD NORWICH 100</Party>
                        <Address>
                              <AddressLine>xxxx</AddressLine>
                              <AddressLine>xxx</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>xxxx</AddressLine>
                        </Address>
                        <Contact>
                              <Name>xxx</Name>
                              <Department>Purchasing</Department>
                              <DDI></DDI>
                        </Contact>
                  </Buyer>
                  <Delivery>
                        <DeliverTo>
                              <DeliverToReferences>
                                    <BuyersCodeForDelivery>xxxx</BuyersCodeForDelivery>
                                    <GLN>3xxx</GLN>
                              </DeliverToReferences>
                              <Party>xxxx</Party>
                              <Address>
                                    <AddressLine>xxxx</AddressLine>
                                    <AddressLine>xxxx</AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine>xxxx</AddressLine>
                              </Address>
                              <Contact>
                                    <Name></Name>
                                    <Department>Purchasing</Department>
                                    <DDI></DDI>
                              </Contact>
                        </DeliverTo>
                  </Delivery>
                  <InvoiceTo>
                        <InvoiceToReferences>
                              <GLN>xxxx</GLN>
                        </InvoiceToReferences>
                        <Party>xxxx</Party>
                        <Address>
                              <AddressLine>xxxx</AddressLine>
                              <AddressLine>NORWICH</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>xxxx</AddressLine>
                        </Address>
                        <Contact>
                              <Name>BILLY/MATT</Name>
                              <Department>Purchasing</Department>
                              <DDI></DDI>
                        </Contact>
                  </InvoiceTo>
                  <OrderLine>
                        <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxx</BuyersProductCode>
                              <Description>xxx</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>3</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>11.96</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>35.88</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                        <LineNumber>2</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxx</BuyersProductCode>
                              <Description>xxxxxxrt List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                        <LineNumber>3</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>Bxxxx</BuyersProductCode>
                              <Description>xxx-</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>17.19</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>17.19</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                        <LineNumber>4</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxx</BuyersProductCode>
                              <Description>xxxxx</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>40.91</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>40.91</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  
                  
            </Order>
      </body>
</biztalk_1>



**********************************
Class Written so far
**********************************
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;


namespace MAMXml
{
    [XmlRoot("biztalk_1", Namespace = "urn:schemas-biztalk-org:biztalk:biztalk_1")]

    public class biztalk_1
    {
        public body body;
    }
    public class body
    {
        [XmlElement("Order", Namespace = "urn:www.basda.org/schema/eBIS-XML_order_v3.00.xml")]
        public Order Order;
    }

    public class Order
    {
        public OrderReferences OrderReferences;
        private string _OrderDate;
        public Supplier Supplier;
        public Buyer Buyer;
        public Delivery Delivery;
        public InvoiceTo InvoiceTo;

       // public List<OrderLine> OrderLine = new List<OrderLine>();
        [XmlArray("OrderLine"), XmlArrayItem("OrderLine", typeof(OrderLine))]
        public System.Collections.ArrayList OrderLine = new System.Collections.ArrayList();
     
             
        public string OrderDate
        {
            set { _OrderDate = value; }
            get { return _OrderDate; }
        }
    }
    public class OrderReferences
    {
        private string _BuyersOrderNumber;

        public string BuyersOrderNumber
        {
            set { _BuyersOrderNumber = value; }
            get { return _BuyersOrderNumber; }
        }
    }

    public class Supplier
    {
        public SupplierReferences SupplierReferences;
        private string _Party;
        [XmlArray("Address"), XmlArrayItem("AddressLine", typeof(string))]
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
    public class SupplierReferences
    {
        private string _BuyersCodeForSupplier;

        public string BuyersCodeForSupplier
        {
            set { _BuyersCodeForSupplier = value; }
            get { return _BuyersCodeForSupplier; }
        }
    }
    public class Contact
    {
        private string _Name;
        private string _Department;
        private int _DID;

        public string Name
        {
            set { _Name = value; }
            get { return _Name; }
        }
        public string Department
        {
            set { _Department = value; }
            get { return _Department; }
        }
        public int DID
        {
            set { _DID = value; }
            get { return _DID; }
        }

    }

    public class Buyer
    {
        public BuyerReferences BuyerReferences;
        private string _Party;
        [XmlArray("Address"), XmlArrayItem("AddressLine", typeof(string))]
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
    public class BuyerReferences
    {
        private string _SupplierCodeForSupplier;

        public string SupplierCodeForSupplier
        {
            set { _SupplierCodeForSupplier = value; }
            get { return _SupplierCodeForSupplier; }
        }
    }

    public class Delivery
    {
        public DeliverTo DeliverTo;
    }
    public class DeliverTo
    {
        public DelivertoReferences DelivertoReferences;
        private string _Party;
        [XmlArray("Address"), XmlArrayItem("AddressLine", typeof(string))]
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
    public class DelivertoReferences
    {
        private string _BuyersCodeForSupplier;

        public string BuyersCodeForSupplier
        {
            set { _BuyersCodeForSupplier = value; }
            get { return _BuyersCodeForSupplier; }
        }
    }

    public class InvoiceTo
    {
        private string _Party;
        [XmlArray("Address"), XmlArrayItem("AddressLine", typeof(string))]
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
       
    public class OrderLine
    {
        private int _LineNumber;
        private Product _Product;
        private Quantity _Quantity;
       
        public int LineNumber
        {
            set { _LineNumber = value; }
            get { return _LineNumber; }
        }
        public Product Product
        {
            set { _Product = value; }
            get { return _Product; }
        }
        public Quantity Quantity
        {
            set { _Quantity = value; }
            get { return _Quantity; }
        }  
    }
    public class Product
    {
        private string _BuyersProductCode;
        private string _Description;

        public string BuyersProductCode
        {
            set { _BuyersProductCode = value; }
            get { return _BuyersProductCode; }
        }
        public string Description
        {
            set { _Description = value; }
            get { return _Description; }
        }
    }
    public class Quantity
    {
        private int _Amount;
        public int Amount
        {
            set { _Amount = value; }
            get { return _Amount; }
        }
    }
   
   
********************************************

Now it gets confising to deserialize  OrderLine ?
0
 
saraganiCommented:
Well, that complicates stuff... There is no name for the class or list that contains the OrderLines
(You just have some OrderLines in the Order, but the name of the list is not mentioned anywhere).

If you had it like that then it would be easy:
<Order>
    <OrderLines>
        <OrderLine>
        </OrderLine>

        <OrderLine>
        </OrderLine>

        <OrderLine>
        </OrderLine>

    </OrderLines>
</Order>

(A list called OderLines contains OrderLines objects).


if the OrderLine array/list is the ONLY list you have in your Order object then you might be able to do this with an Indexer.
I will try to write you an example soon.
0
 
Anurag ThakurTechnical ManagerCommented:
microsoft gives the following sample to do deserialization of an array
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlarray.aspx
0
 
WannabeNerdAuthor Commented:
Well saragani you are right it gets a bit complicated.
Here is an examples which i am trying to learn from :
***********************************************************
http://www.developerfusion.com/code/4639/customize-xml-serialization-using-ixmlserializable/

***********************************************************
0
 
saraganiCommented:
Well, indexer doesn't help... I tried to do:
public class Order
    {
        public OrderReferences OrderReferences;
        private string _OrderDate;
        public Supplier Supplier;
        public Buyer Buyer;
        public Delivery Delivery;
        public InvoiceTo InvoiceTo;

        private List<OrderLine> OrderLines = new List<OrderLine>();

        public OrderLine this[int index]
        {
            get
            {
                return OrderLines[index];
            }
            set
            {
                OrderLines[index] = value;
            }
        }


        public string OrderDate
        {
            set { _OrderDate = value; }
            get { return _OrderDate; }
        }
    }

(Which in this way you can do Order[5])

I think that you will have to change your XML structure a little or serialize it manualy (which sucks)
0
 
WannabeNerdAuthor Commented:
Hi,
 I know the XMl structure should be changed but to be honest its not my XML. i have to get it from customers and then read so there lies the problem.
I think i have to serialize it manualy.It sucks but i have to do  it.
I'll have a look but could not find any example at all.
0
 
saraganiCommented:
Well, there is another solution which I haven't took under consideration, and just like the other idea with the Indexer, it will work  IF AND ONLY IF the order object contains only one list (Which in this case, list of OrderLine).

The problem that we had in the XML is that there is no list container... There is Order, and then inside it there are OrderLines (Instead of having OrderLines inside some kind of a list inside Order)...

But what if Order will be a list??

What I did is this:

    public class body
    {
        public Order<OrderLine> Order;
    }

    public class Order<T> : List<T>
    {
        public OrderReferences OrderReferences;
        private string _OrderDate;
        public Supplier Supplier;
        public Buyer Buyer;
        public Delivery Delivery;
        public InvoiceTo InvoiceTo;

        public string OrderDate
        {
            set { _OrderDate = value; }
            get { return _OrderDate; }
        }
    }






I have allowed myself to remove the xmlns and the namespace from the XML and classes since my project doesn't contain any name spaces like it.

I've tested it and it works.... The full XML is:

<biztalk_1>
      <header>
            <delivery>
                  <message/>
                  <to>
                        <address/>
                  </to>
                  <from>
                        <address/>
                  </from>
            </delivery>
            <manifest>
                  <document>
                        <name>Order</name>
                        <description>EBis Order</description>
                  </document>
            </manifest>
      </header>
      <body>
            <Order>
                  <OrderHead>
                        <Schema>
                              <Version>3.00</Version>
                        </Schema>
                        <Stylesheet>
                              <StylesheetOwner/>
                              <StylesheetName>eBIS_MAM.xsl</StylesheetName>
                              <Version/>
                              <StylesheetType>xsl</StylesheetType>
                        </Stylesheet>
                        <Parameters>
                              <Language>en_GB</Language>
                              <DecimalSeparator>.</DecimalSeparator>
                              <Precision>20.2</Precision>
                        </Parameters>
                        <OriginatingSoftware>
                              <SoftwareManufacturer>MAM</SoftwareManufacturer>
                              <SoftwareProduct>Autopart</SoftwareProduct>
                              <SoftwareVersion>17 YL</SoftwareVersion>
                        </OriginatingSoftware>
                        <OrderType Code="PUO"/>
                        <Function Code="FIO"/>
                        <OrderCurrency>
                              <Currency Code="GBP"/>
                        </OrderCurrency>
                        <Checksum>36862</Checksum>
                  </OrderHead>
                  <OrderReferences>
                        <BuyersOrderNumber>AP115502</BuyersOrderNumber>
                  </OrderReferences>
                  <OrderDate>2008-12-04T13:35</OrderDate>
                  <Supplier>
                        <SupplierReferences>
                              <BuyersCodeForSupplier>xxx</BuyersCodeForSupplier>
                        </SupplierReferences>
                        <Party>xxx</Party>
                        <Address>
                              <AddressLine>xxxL</AddressLine>
                              <AddressLine>xxx.</AddressLine>
                              <AddressLine>xxx</AddressLine>
                              <AddressLine>xxx</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>xxx</AddressLine>
                        </Address>
                        <Contact>
                              <Name>-</Name>
                              <Department>Sales</Department>
                              <DDI>xxxxx</DDI>
                        </Contact>
                  </Supplier>
                  <Buyer>
                        <BuyerReferences>
                              <SuppliersCodeForBuyer>xxxx</SuppliersCodeForBuyer>
                              <GLN>3UE372961233NR3</GLN>
                        </BuyerReferences>
                        <Party>EU LTD NORWICH 100</Party>
                        <Address>
                              <AddressLine>xxxx</AddressLine>
                              <AddressLine>xxx</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>xxxx</AddressLine>
                        </Address>
                        <Contact>
                              <Name>xxx</Name>
                              <Department>Purchasing</Department>
                              <DDI></DDI>
                        </Contact>
                  </Buyer>
                  <Delivery>
                        <DeliverTo>
                              <DeliverToReferences>
                                    <BuyersCodeForDelivery>xxxx</BuyersCodeForDelivery>
                                    <GLN>3xxx</GLN>
                              </DeliverToReferences>
                              <Party>xxxx</Party>
                              <Address>
                                    <AddressLine>xxxx</AddressLine>
                                    <AddressLine>xxxx</AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine>xxxx</AddressLine>
                              </Address>
                              <Contact>
                                    <Name></Name>
                                    <Department>Purchasing</Department>
                                    <DDI></DDI>
                              </Contact>
                        </DeliverTo>
                  </Delivery>
                  <InvoiceTo>
                        <InvoiceToReferences>
                              <GLN>xxxx</GLN>
                        </InvoiceToReferences>
                        <Party>xxxx</Party>
                        <Address>
                              <AddressLine>xxxx</AddressLine>
                              <AddressLine>NORWICH</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>xxxx</AddressLine>
                        </Address>
                        <Contact>
                              <Name>BILLY/MATT</Name>
                              <Department>Purchasing</Department>
                              <DDI></DDI>
                        </Contact>
                  </InvoiceTo>
                  <OrderLine>
                        <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxx</BuyersProductCode>
                              <Description>xxx</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>3</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>11.96</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>35.88</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                        <LineNumber>2</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxx</BuyersProductCode>
                              <Description>xxxxxxrt List</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>25.33</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>25.33</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                        <LineNumber>3</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>Bxxxx</BuyersProductCode>
                              <Description>xxx-</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>17.19</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>17.19</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                  <OrderLine>
                        <LineNumber>4</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxx</BuyersProductCode>
                              <Description>xxxxx</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>1</Amount>
                        </Quantity>
                        <Price>
                              <UnitPrice>40.91</UnitPrice>
                        </Price>
                        <LineTax>
                              <TaxRate Code="s">15</TaxRate>
                              <TaxValue>0</TaxValue>
                        </LineTax>
                        <LineTotal>40.91</LineTotal>
                        <Delivery>
                              <DeliverTo>
                                    <Party/>
                              </DeliverTo>
                              <PreferredDate>2008-12-05</PreferredDate>
                        </Delivery>
                  </OrderLine>
                 
                 
            </Order>
      </body>
</biztalk_1>



The Classes are:

    public class biztalk_1
    {
        public body body;
    }
    public class body
    {
        public Order<OrderLine> Order;
    }

    public class Order<T> : List<T>
    {
        public OrderReferences OrderReferences;
        private string _OrderDate;
        public Supplier Supplier;
        public Buyer Buyer;
        public Delivery Delivery;
        public InvoiceTo InvoiceTo;

        public string OrderDate
        {
            set { _OrderDate = value; }
            get { return _OrderDate; }
        }
    }

    public class OrderReferences
    {
        private string _BuyersOrderNumber;

        public string BuyersOrderNumber
        {
            set { _BuyersOrderNumber = value; }
            get { return _BuyersOrderNumber; }
        }
    }

    public class Supplier
    {
        public SupplierReferences SupplierReferences;
        private string _Party;
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
    public class SupplierReferences
    {
        private string _BuyersCodeForSupplier;

        public string BuyersCodeForSupplier
        {
            set { _BuyersCodeForSupplier = value; }
            get { return _BuyersCodeForSupplier; }
        }
    }
    public class Contact
    {
        private string _Name;
        private string _Department;
        private int _DID;

        public string Name
        {
            set { _Name = value; }
            get { return _Name; }
        }
        public string Department
        {
            set { _Department = value; }
            get { return _Department; }
        }
        public int DID
        {
            set { _DID = value; }
            get { return _DID; }
        }

    }

    public class Buyer
    {
        public BuyerReferences BuyerReferences;
        private string _Party;
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
    public class BuyerReferences
    {
        private string _SupplierCodeForSupplier;

        public string SupplierCodeForSupplier
        {
            set { _SupplierCodeForSupplier = value; }
            get { return _SupplierCodeForSupplier; }
        }
    }

    public class Delivery
    {
        public DeliverTo DeliverTo;
    }
    public class DeliverTo
    {
        public DelivertoReferences DelivertoReferences;
        private string _Party;
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }
    public class DelivertoReferences
    {
        private string _BuyersCodeForSupplier;

        public string BuyersCodeForSupplier
        {
            set { _BuyersCodeForSupplier = value; }
            get { return _BuyersCodeForSupplier; }
        }
    }

    public class InvoiceTo
    {
        private string _Party;
        public System.Collections.ArrayList Address = new System.Collections.ArrayList();
        public Contact Contact;

        public string Party
        {
            set { _Party = value; }
            get { return _Party; }
        }
    }

    public class OrderLine
    {
        private int _LineNumber;
        private Product _Product;
        private Quantity _Quantity;

        public int LineNumber
        {
            set { _LineNumber = value; }
            get { return _LineNumber; }
        }
        public Product Product
        {
            set { _Product = value; }
            get { return _Product; }
        }
        public Quantity Quantity
        {
            set { _Quantity = value; }
            get { return _Quantity; }
        }
    }
    public class Product
    {
        private string _BuyersProductCode;
        private string _Description;

        public string BuyersProductCode
        {
            set { _BuyersProductCode = value; }
            get { return _BuyersProductCode; }
        }
        public string Description
        {
            set { _Description = value; }
            get { return _Description; }
        }
    }
    public class Quantity
    {
        private int _Amount;
        public int Amount
        {
            set { _Amount = value; }
            get { return _Amount; }
        }
    }




And when I serialize it by:
XmlSerializer xs = new XmlSerializer(typeof(biztalk_1));
            biztalk_1 bt;
            using (FileStream fs = new FileStream(@"c:\1.xml", FileMode.Open))
            {
                bt = (biztalk_1)xs.Deserialize(fs);
            }

I can do:
bt.body.Order[4].LineNumber
(Which Order.Orderline[4].LineNumber)

And I can also do:
bt.body.Order.OrderReferences.BuyersOrderNumber


:-)

0
 
saraganiCommented:
Well, I've tested it again and it doesn't work properly.
The items on the list get filled, but all the other data is not... so in this case you will not have OrderDate etc...

I wonder why it does that. Maybe you should not inherit from List<T> but create your own List (by deriving from IEnumerable etc)
0
 
WannabeNerdAuthor Commented:
0
 
WannabeNerdAuthor Commented:
i'll also have a look if i can live without OrderData
0
 
WannabeNerdAuthor Commented:
Nope it does not work. Tried  it and with namespace it gives me errors.
0
 
saraganiCommented:
It not just OrderDate which is missing.
It seems that anything that derives from collection (also every class that you make that implements IEnumerable) will only have the elements in the list serialized...


You can use IXmlSerializable, but you will need to implement the serialization manually...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.