Solved

Read XML file

Posted on 2006-11-30
7
300 Views
Last Modified: 2012-05-05
I am trying to use c#2005 to extract data from an XML file in the format below.  The Data I need is the Lot Number (if it exists), the Expiry date, and the Lot qty / store.

Any ideas how to go about doing this bearing in mind that it is quite new to  me.

Thank you,

David


- <INVENTORYS>
- <INVENTORY>
  <INVN_STYLE style_sid="4775374644528021744" />
  <INVN item_sid="4775374643865321712" upc="" use_qty_decimals="2" />
- <INVN_SBS sbs_no="1" alu="" dcs_code="BEV01 02" vend_code="T002" scale_no="" description1="LFR 33CL B*24" description2="4650" description3="" description4="LEFFE RADIEUSE" attr="" siz="9S9S" cost="0" spif="0" fc_cost="0" fst_rcvd_date="2006-11-29T00:00:00" lst_rcvd_date="2006-11-29T00:00:00" lst_sold_date="2006-11-10T00:00:00" marked_date="2006-10-29T00:00:00" discont_date="" created_date="2006-11-29T00:00:00" modified_date="2006-11-29T16:56:40+01:00" tax_code="0" comm_code="" sched_no="" fst_price="0" markdown_price="0" qty_per_case="24" lst_rcvd_cost="0" flag="0" ext_flag="4" edi_flag="0" kit_type="0" max_disc_perc1="0" max_disc_perc2="0" min_ord_qty="0" vend_lead_time="" vend_list_cost="0" trade_disc_perc="0" udf1_date="" udf2_value="" unorderable="0" print_tag="1" active="1" mark_for_del="0" item_no="1" cms="0" currency_name="">
- <INVN_SBS_SUPPLS>
  <INVN_SBS_SUPPL udf_no="7" udf_value="" />
  <INVN_SBS_SUPPL udf_no="8" udf_value="" />
  </INVN_SBS_SUPPLS>
  <INVN_SERIALS />
- <INVN_SBS_VENDORS>
  <INVN_SBS_VENDOR vend_no="1" vend_code="B004" upc="1111234567892" alu="12345678901234567892" lst_rcvd_cost="0" />
  <INVN_SBS_VENDOR vend_no="2" vend_code="" upc="1111234567893" alu="12345678901234567893" lst_rcvd_cost="0" />
  <INVN_SBS_VENDOR vend_no="3" vend_code="" upc="1111234567894" alu="12345678901234567894" lst_rcvd_cost="0" />
  <INVN_SBS_VENDOR vend_no="4" vend_code="" upc="1111234567895" alu="12345678901234567895" lst_rcvd_cost="0" />
  <INVN_SBS_VENDOR vend_no="5" vend_code="" upc="1111234567896" alu="12345678901234567896" lst_rcvd_cost="0" />
  <INVN_SBS_VENDOR vend_no="6" vend_code="" upc="1111234567897" alu="12345678901234567897" lst_rcvd_cost="0" />
  </INVN_SBS_VENDORS>
  <INVN_SBS_SUBLOCS />
  <INVN_SBS_PRICES />
- <INVN_SBS_QTYS>
  <INVN_SBS_QTY store_no="0" qty="0" min_qty="0" max_qty="0" transfer_in_qty="0" transfer_out_qty="0" sold_qty="0" rcvd_qty="0" onorder_qty="0" />
  <INVN_SBS_QTY store_no="1" qty="7849" min_qty="0" max_qty="0" transfer_in_qty="0" transfer_out_qty="0" sold_qty="0" rcvd_qty="0" onorder_qty="0" />
  <INVN_SBS_QTY store_no="250" qty="" min_qty="0" max_qty="0" transfer_in_qty="" transfer_out_qty="" sold_qty="" rcvd_qty="" onorder_qty="" />
  </INVN_SBS_QTYS>
  <INVN_SBS_KITS />

//////////////////////////////////////// I need this Data //////////////////////////////////////////////////////////////////////////
- <LOTS>
- <LOT lot_number="1234" expiry_date="2006-12-01T00:00:00" created_date="2006-11-29T12:45:44" modified_date="2006-11-29T13:43:26" active="1">
- <LOT_QTYS>
  <LOT_QTY store_no="0" qty="0" />
  <LOT_QTY store_no="1" qty="147" />
  </LOT_QTYS>
  </LOT>
- <LOT lot_number="1235" expiry_date="2006-12-05T00:00:00" created_date="2006-11-29T12:45:54" modified_date="2006-11-29T13:43:26" active="1">
- <LOT_QTYS>
  <LOT_QTY store_no="0" qty="0" />
  <LOT_QTY store_no="1" qty="702" />
  </LOT_QTYS>
  </LOT>
- <LOT lot_number="8231" expiry_date="2006-12-07T00:00:00" created_date="2006-11-29T12:46:03" modified_date="2006-11-29T13:43:26" active="1">
- <LOT_QTYS>
  <LOT_QTY store_no="0" qty="0" />
  <LOT_QTY store_no="1" qty="36" />
  </LOT_QTYS>
  </LOT>
- <LOT lot_number="28737" expiry_date="2006-12-19T00:00:00" created_date="2006-11-29T12:46:15" modified_date="2006-11-29T13:43:26" active="1">
- <LOT_QTYS>
  <LOT_QTY store_no="0" qty="0" />
  <LOT_QTY store_no="1" qty="6969" />
  </LOT_QTYS>
  </LOT>
  </LOTS>
//////////////////////////////////////////////////////////////////////////////////////////////////

  </INVN_SBS>
  </INVENTORY>
- <INVENTORY>
  <INVN_STYLE style_sid="4775374650492322032" />
0
Comment
Question by:dbdp
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:bromose
ID: 18047841
Hi
I have made some code for you:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace ArvOpg1
{
    class Demo
    {
        public void Demo()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("c:\\MyXml.xml");
            XmlNodeList nodes = doc.SelectNodes("//LOT[./@lot_number != '']");
            foreach (XmlNode node in nodes)
            {
                string lot_nr = node.SelectSingleNode("./@lot_number").InnerText;
                string expiry_date = node.SelectSingleNode("./@expiry_date").InnerText;
                XmlNodeList nodes2 = node.SelectNodes("./LOT_QTY");
                foreach (XmlNode node2 in nodes2)
                {
                    string store_no = node.SelectSingleNode("./@store_no").InnerText;
                    string qty = node.SelectSingleNode("./@qty").InnerText;
                }
            }
        }
    }
}

Best regards, Kaj Bromose
0
 

Author Comment

by:dbdp
ID: 18048414
Hello Kaj,

Thank you very much already but I forgot one important piece - I also need the item_no (in this case item_no = '1'

Can you help?
0
 

Author Comment

by:dbdp
ID: 18048596
Also  IDEALLY I would like to add the data to a datatable/dataset - therefore the table would be :


Item | Lot No | Expiry date | Store No  |Lot Qty
1      |1234    |2006-12-01 |0              |0
1      |1234    |2006-12-01 |1              |147
1      |1235    |2006-12-05 |0              |0  
1      |1235    |2006-12-05 |1              |702
...
etc
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:bromose
ID: 18048646
Hi David

I hope my x-path is correct ;-) - here are the corrected code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ArvOpg1
{
    class Demo
    {
        public void Demo2()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("c:\\MyXml.xml");
            XmlNodeList nodes = doc.SelectNodes("//LOT[./@lot_number != '']");
            foreach (XmlNode node in nodes)
            {
                string item_no = node.SelectSingleNode("../../INVN_SBS/@item_no").InnerText; //Parent/Parent/INVN_SBS node
                string lot_nr = node.SelectSingleNode("./@lot_number").InnerText;
                string expiry_date = node.SelectSingleNode("./@expiry_date").InnerText;
                XmlNodeList nodes2 = node.SelectNodes("./LOT_QTY");
                foreach (XmlNode node2 in nodes2)
                {
                    string store_no = node.SelectSingleNode("./@store_no").InnerText;
                    string qty = node.SelectSingleNode("./@qty").InnerText;
                }



            }
        }
    }
}

I could do the datatable for you, but I will take a littel time.
In fact I think the best solution would be to make a XSL Stylesheet, and style a document, which easily is read into a dataset.

Best regards, Kaj Bromose
0
 
LVL 3

Expert Comment

by:bromose
ID: 18048749
Hi Davis

Here is a quick and dirty:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace ArvOpg1
{
    class Demo
    {
        public void Demo2()
        {
            DataTable myTable = new DataTable();
            //Item | Lot No | Expiry date | Store No  |Lot Qty
            myTable.Columns.Add(new DataColumn("Item", new string().GetType()));
            myTable.Columns.Add(new DataColumn("Lot No", new string().GetType()));
            myTable.Columns.Add(new DataColumn("Expiry date", new string().GetType()));
            myTable.Columns.Add(new DataColumn("Store No", new string().GetType()));
            myTable.Columns.Add(new DataColumn("Lot Qty", new string().GetType()));

            XmlDocument doc = new XmlDocument();
            doc.Load("c:\\MyXml.xml");
            XmlNodeList nodes = doc.SelectNodes("//LOT[./@lot_number != '']");
            foreach (XmlNode node in nodes)
            {
                string item_no = node.SelectSingleNode("../../INVN_SBS/@item_no").InnerText;
                string lot_nr = node.SelectSingleNode("./@lot_number").InnerText;
                string expiry_date = node.SelectSingleNode("./@expiry_date").InnerText;
                XmlNodeList nodes2 = node.SelectNodes("./LOT_QTY");
                foreach (XmlNode node2 in nodes2)
                {
                    string store_no = node.SelectSingleNode("./@store_no").InnerText;
                    string qty = node.SelectSingleNode("./@qty").InnerText;
                    DataRow row = myTable.NewRow();
                    row["Item"] = item_no;
                    row["Lot No"] = lot_nr;
                    row["Expiry date"] = expiry_date;
                    row["Store No"] = store_no;
                    row["Lot Qty"] = qty;
                    myTable.Rows.Add(row);
                }



            }
        }
    }
}
0
 
LVL 3

Accepted Solution

by:
bromose earned 500 total points
ID: 18049109
Hi David

I have now a tested and running version :-)

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace ArvOpg1
{
    class Demo
    {
        public void Demo2()
        {
            DataTable myTable = new DataTable();
            //Item | Lot No | Expiry date | Store No  |Lot Qty
            myTable.Columns.Add(new DataColumn("Item", "a".GetType()));
            myTable.Columns.Add(new DataColumn("Lot No", "a".GetType()));
            myTable.Columns.Add(new DataColumn("Expiry date", "a".GetType()));
            myTable.Columns.Add(new DataColumn("Store No", "a".GetType()));
            myTable.Columns.Add(new DataColumn("Lot Qty", "a".GetType()));

            XmlDocument doc = new XmlDocument();
            doc.Load("c:\\MyXml.xml");
            XmlNodeList nodes = doc.SelectNodes("//LOT[./@lot_number != '']");
            foreach (XmlNode node in nodes)
            {
                string item_no = node.SelectSingleNode("../../@item_no").InnerText;
                string lot_nr = node.SelectSingleNode("./@lot_number").InnerText;
                string expiry_date = node.SelectSingleNode("./@expiry_date").InnerText;
                XmlNodeList nodes2 = node.SelectNodes("./LOT_QTYS/LOT_QTY");
                foreach (XmlNode node2 in nodes2)
                {
                    string store_no = node2.SelectSingleNode("./@store_no").InnerText;
                    string qty = node2.SelectSingleNode("./@qty").InnerText;
                    DataRow row = myTable.NewRow();
                    row["Item"] = item_no;
                    row["Lot No"] = lot_nr;
                    row["Expiry date"] = expiry_date;
                    row["Store No"] = store_no;
                    row["Lot Qty"] = qty;
                    myTable.Rows.Add(row);
                }



            }
        }
    }
}


0
 

Author Comment

by:dbdp
ID: 18049807
That's really Excellent,

thank you so much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

21 Experts available now in Live!

Get 1:1 Help Now