473,503 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best approach to deserializing nested elements.

I am working on an order entry program and have a question related to
deserializing nodes with nested elements.

The purchase order contains multiple line items which I select using an
XmlNodeList. I am trying to deserialize the nodes using a foreach as follows:

foreach(XmlNode lineItem in LineItemsNodeList)

An abbreviated example of the nested lineItem node looks like this:

<LineItem>
<OrderLine>
<LineSequenceNumber>1</LineSequenceNumber1>
<PartQualifier1>BP</PartQualifier1>
<PartNumber1>BTI318</PartNumber1>
<OrderQuantity>6</OrderQuantity>
<UnitPrice>12.50</UnitPrice>
</OrderLiine>
<Reference>
<Description>HAPPY BIRTHDAY JOHN</Description>
</Reference>
</LineItem>

I have created an object to represent the Xml node with
[XmlRoot("LineItem")] and [XmlElement("ElementName")] for each of the other
elements.

I read the element into an XmlReader, create a serializer object, and then
deserialize the node.

My question is, should I deserialize the entire nested node, or should I
create two seperate objects to deserialize: One for the <OrderLinesection,
and one for the <Referencesection?

When trying to deserialize the entire nested node, the value of
<LineSequenceNumbergets assigned to <OrderLine>. What am I doing wrong?
What is the correct way to deal with this?

--
Jeremy
--
Jeremy
Apr 4 '07 #1
1 11761

"Jeremy_B" wrote:
I am working on an order entry program and have a question related to
deserializing nodes with nested elements.

The purchase order contains multiple line items which I select using an
XmlNodeList. I am trying to deserialize the nodes using a foreach as follows:

foreach(XmlNode lineItem in LineItemsNodeList)

An abbreviated example of the nested lineItem node looks like this:

<LineItem>
<OrderLine>
<LineSequenceNumber>1</LineSequenceNumber1>
<PartQualifier1>BP</PartQualifier1>
<PartNumber1>BTI318</PartNumber1>
<OrderQuantity>6</OrderQuantity>
<UnitPrice>12.50</UnitPrice>
</OrderLiine>
<Reference>
<Description>HAPPY BIRTHDAY JOHN</Description>
</Reference>
</LineItem>

I have created an object to represent the Xml node with
[XmlRoot("LineItem")] and [XmlElement("ElementName")] for each of the other
elements.

I read the element into an XmlReader, create a serializer object, and then
deserialize the node.

My question is, should I deserialize the entire nested node, or should I
create two seperate objects to deserialize: One for the <OrderLinesection,
and one for the <Referencesection?

When trying to deserialize the entire nested node, the value of
<LineSequenceNumbergets assigned to <OrderLine>. What am I doing wrong?
What is the correct way to deal with this?

--
Jeremy
I found a great example on MSDN that pointed me in the right direction
(scroll to the bottom for the PO example.
http://msdn2.microsoft.com/en-us/lib...wa(vs.80).aspx

The trick for deserializing the Xml nodes with nested elements (complex
types) is to create a class for each node <OrderLineand <Reference>, as
well as a class for the complex node <LineItem>. The class for <LineItem>
contains the object classes for the <OrderLineand <Reference>. Below is an
example of the classes.

[XmlRootAttribute("LineItem")]
public class LineItemComplete
{
[XmlElement("OrderLine")]
public SerOrderLine orderLine;

[XmlElement("Reference")]
public SerLineRef lineRef;
}
public class SerLineRef
{
private string _description;

public SerLineRef()
{
}

public string Description
{
get { return _description; }
set { _description = value; }
}
}

public class SerOrderLine
{
public string LineSequenceNumber;
public string PartNumberQualifier1;
public string PartNumber1;
public string OrderQty;
public string OrderQtyUOM;
public string UnitPrice;
}

For berevity sake, I did not put the Get Set methods on the class
SerOrderLine, but it would be exactly the same as I put for the SerLinRef
class.

When I deserialze the node, it is done with a type of LineItemComplete.

Hope this helps with anyone else who may be searching.

Jeremy
Apr 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8308
by: Stanimir Stamenkov | last post by:
Here's an example: <form action="bogus" method="post"> <p> <a href="prev.cgi"><input type="submit" name="prev" value="< Back"></a> <a href="next.cgi"><input type="submit" name="next"...
5
2806
by: horndude77 | last post by:
Ok, this might be for a web designing group, but here's my problem. I'm trying to make a web page with tabs which you can navigate between without the page reloading. I have one set of tabs working...
4
7499
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
46
9868
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
1
1437
by: Bob Rock | last post by:
Hello, always having to validate an XML stream against a XSD may add up an important overhead. My XMLs are usually the result of serializing a class instance and often in my applications what I...
7
16046
by: farseer | last post by:
Hi, i am getting an error when trying to deserialize and xml. the error is shown below. i get this error regardless if i try to desearialize in C# or VB. the XML itself does infact contain a...
2
1834
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
1
2320
by: jcsnippets.atspace.com | last post by:
Hi everyone, I'm trying to read an xml config file in C#. To do this, I have used the xsd.exe tool to create an xsd file from my xml, and to generate code to read this xml file. Here is a...
4
1826
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.