Solved

Read XML file

Posted on 2006-11-30
7
302 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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