Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Read XML file

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
dbdp
Asked:
dbdp
  • 4
  • 3
1 Solution
 
bromoseCommented:
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
 
dbdpAuthor Commented:
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
 
dbdpAuthor Commented:
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
Independent Software Vendors: 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!

 
bromoseCommented:
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
 
bromoseCommented:
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
 
bromoseCommented:
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
 
dbdpAuthor Commented:
That's really Excellent,

thank you so much
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now