Solved

Deserialization XMl file

Posted on 2009-05-13
6
479 Views
Last Modified: 2013-12-17
I am writing c# classes to deserialize xml file.
I am not able to write a class for the part below :
The problem is that OrderLine exists more then once and i want to deserialize order line to an array or something.

Any help will be appreciated

**********************************************************
<ORDER>
<OrderLine>
                        <LineNumber>1</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxx</BuyersProductCode>
                              <Description>xxxx7</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>3</Amount>
                        </Quantity>
                                                                                                      
                        </Delivery>
                  </OrderLine>

<OrderLine>
                        <LineNumber>2</LineNumber>
                        <Product>
                              <SuppliersProductCode/>
                              <BuyersProductCode>xxxx</BuyersProductCode>
                              <Description>xxxx7</Description>
                        </Product>
                        <Quantity>
                              <Packsize>EACH</Packsize>
                              <Amount>3</Amount>
                        </Quantity>
                                                                                                      
                        </Delivery>
                  </OrderLine>
</ORDER>
public class Order

    {

           public 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
  • 4
  • 2
6 Comments
 
LVL 3

Expert Comment

by:steeza
ID: 24372772
How about:


public class Order

    {
 

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

        public List<OrderLine> OrderLineList 

        {

            set {_orderLineList = value; }

            get { return _orderLineList; }

        }

    }
 

//Or
 

public class Order

    {
 

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

        public void AddOrderLine(Order order)

        {

         this._orderLineList.Insert(index,orderLine.LineNumber)

        }
 

        public Order AddOrderLine(int index)

        {

         return this._orderLineList[index]);

        }

        

        //add other accessor/modifier methods as appropriate
 

    }
 

//Didn't check this in a compiler so there might be spelling mistakes etc.

Open in new window

0
 
LVL 3

Expert Comment

by:steeza
ID: 24372789
Some nasty errors in there, this version is better.
public class Order

    {

 

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

 

        public void AddOrderLine(OrderLine orderLine)

        {

         this._orderLineList.Insert(orderLine.LineNumber,orderLine)

        }

 

        public Order GetOrderLine(int index)

        {

         return this._orderLineList[index]);

        }

        

        //add other accessor/modifier methods as appropriate

 

    }

Open in new window

0
 
LVL 3

Expert Comment

by:steeza
ID: 24372793
This is what happens when answering questions while being on the phone ;)
public class Order

    {

 

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

 

        public void AddOrderLine(OrderLine orderLine)

        {

         this._orderLineList.Insert(orderLine.LineNumber,orderLine)

        }

 

        public OrderLine GetOrderLine(int index)

        {

         return this._orderLineList[index]);

        }

        

        //add other accessor/modifier methods as appropriate

 

    }

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:WannabeNerd
ID: 24373204
well then how to deserialize it and get orderline items ?
0
 
LVL 3

Expert Comment

by:steeza
ID: 24373734
If you are looking for a tutorial on xml deserialization in C# I'm not going to get into that myself. You can find loads of these on the web:

http://mehranikoo.net/CS/archive/2007/02/05/SerialisationAndEncodingInWCF.aspx
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.deserialize(VS.71).aspx

or someone else here might help you.

I was merely giving a suggestion in regards to your class structure based on your statement: "I am not able to write a class for the part below".  

0
 

Accepted Solution

by:
WannabeNerd earned 0 total points
ID: 24374489
Yes i do understand but i want a class which can be serialized and deserialized.
When i read xml it should straight away be deserialized so that i can take data from it.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
coded character set iptc field within a jpeg 1 70
What is Thread.Sleep? 5 46
System.Speech 2 16
C# parent child form 5 19
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now