Avatar of satmisha
satmisha
Flag for India asked on

Read\Write XML (Recurring Tags) Using VB.Net

Hi Expert,

Need to Read XML file having Recurring tags and after reading those I need to insert them in DataBase.

Here is XML file: Here Tags are repeated:

<?xml version="1.0" encoding="utf-8" ?>
<po:PurchaseOrder xmlns:po="http://michalk.com/XmlDOM/PO.xsd"[ccc]
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"[ccc]
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Number>1001</Number>
<OrderDate>8/12/01</OrderDate>
<BillToAddress>
   <Street>101 Main Street</Street>
   <State>NC</State>
   <ZipCode>28273</ZipCode>
</BillToAddress>
<BillToAddress>
    <State>NC</State>
    <ZipCode>28273</ZipCode>
</BillToAddress>
<ShipToAddress>
   <Street>101 Main Street</Street>
   <City>Charlotte</City>
   <State>NC</State>
</ShipToAddress>
  <ShipToAddress>
    <Street>101 Main Street</Street>
    <City>Charlotte</City>
  </ShipToAddress>

</po:PurchaseOrder>

Please suggest.

Looking forward to hearing from you.
Visual Basic.NETXMLC#

Avatar of undefined
Last Comment
Dmitry G

8/22/2022 - Mon
Dmitry G

Please give more info.

Does the xml always contain two ship-to addresses, two bill-to addresses?
Why some addresses have not all information?

What is the structure of your DB? Are there tables for addresses? Orders?
satmisha

ASKER
Thanks for your quick reply.

Consider it as nested XML:
# XML can contain multiple "ship-to addresses", Max would be 99.
# Similarly "two bill-to addresses" is also Dynamic.
# XML is dynamic and Nested in nature and might not contain all the Inner Nodes.
# There is only one Table Which contains all the relevent column if information is not provided like for address as you stated it will contain null for those.

Pls Assist.
Dmitry G

"# There is only one Table Which contains all the relevent column"

Orders table contains up to 99 fields for addresses?

Anyway, let me try with some code...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Dmitry G

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Dmitry G

Output:


==================================
Order Number = 1001
Order Date = 8/12/2001 12:00:00 a.m.
~~~~Bill to Address:
101 Main Street
NC
28273

~~~~Bill to Address:
NC
28273


~~~~Ship to Address:
101 Main Street
NC
28273

~~~~Ship to Address:
NC
28273
satmisha

ASKER
Thanks a lot for the code. I am going to allocate full point to this.

But My requirment is to Read the Nested XML file then validate it and then Insert into the DataBase using vb.net Code. I'll be happy if you cd write some code to achieve that.

Also what wd the better approach .

Should I Deserialize the XML into object , Validate it and then Pass the Data to DataBase to get inserted into Table..?

I am going to Open new thread and seeking your help on it.
Here is the Question: Please assist.

Read Nested XML then Validate and finally Insert the Data in DataTable"
satmisha

ASKER
pls reply.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Dmitry G

Sorry, we probably have some time shift. And I live under Pacific time :)

"Should I Deserialize the XML into object"
Yes, I'd use this approach. really, that's what I wanted to suggest, just tried to keep my code simpler.
So, you need a PurchaseOrder class with properties like:
- Number
- Date
- BillToAddresses
- ShipToAddresses

About validation - it might be a bit more tricky. From one hand, you have an xml schema and you may to validate against the schema.
Validation can also be done at an object level.

Saving into a database should be pretty straightforward. But to write code I need to know your DB structure.

If you are not familiar with C# DB access, try the following resource:
http://www.programmersheaven.com/2/les_csharp_13_p1