Solved

Read XML file

Posted on 2006-11-30
7
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Industry Leaders: 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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