Solved

Deserializations of xml file - IXmlSerializable

Posted on 2009-05-15
19
1,053 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:WannabeNerd
  • 10
  • 6
  • 2
  • +1
19 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 24393734
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
 

Author Comment

by:WannabeNerd
ID: 24393767
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
 

Author Comment

by:WannabeNerd
ID: 24394831
    <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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24396239
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
 

Author Comment

by:WannabeNerd
ID: 24396279
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
 
LVL 11

Expert Comment

by:saragani
ID: 24396374
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
 

Author Comment

by:WannabeNerd
ID: 24396719
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
 
LVL 11

Expert Comment

by:saragani
ID: 24397037
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
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24397108
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
 

Author Comment

by:WannabeNerd
ID: 24397425
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
 
LVL 11

Expert Comment

by:saragani
ID: 24397633
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
 

Author Comment

by:WannabeNerd
ID: 24409843
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
 
LVL 11

Expert Comment

by:saragani
ID: 24411774
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
 
LVL 11

Expert Comment

by:saragani
ID: 24411893
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
 

Author Comment

by:WannabeNerd
ID: 24411954
0
 

Author Comment

by:WannabeNerd
ID: 24411966
i'll also have a look if i can live without OrderData
0
 

Author Comment

by:WannabeNerd
ID: 24412317
Nope it does not work. Tried  it and with namespace it gives me errors.
0
 
LVL 11

Expert Comment

by:saragani
ID: 24412336
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
 

Accepted Solution

by:
WannabeNerd earned 0 total points
ID: 24430378
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question