Apr 26, 2012

How to opean and read xml file in c#.net using for each loop

XML File:



<Table>
<Product0>
<Product>
<Product_id>1</Product_id>
<Product_name>Product 1</Product_name>
<Product_price>1000</Product_price>
</Product>
<Product>
<Product_id>2</Product_id>
<Product_name>Product 2</Product_name>
<Product_price>2000</Product_price>
</Product>
<Product>
<Product_id>3</Product_id>
<Product_name>Product 3</Product_name>
<Product_price>3000</Product_price>
</Product>
<Product>
<Product_id>4</Product_id>
<Product_name>Product 4</Product_name>
<Product_price>4000</Product_price>
</Product>
</Product0>
<Product1>
<Product>
<Product_id>11</Product_id>
<Product_name>Product 11</Product_name>
<Product_price>11000</Product_price>
</Product>
<Product>
<Product_id>21</Product_id>
<Product_name>Product 21</Product_name>
<Product_price>12000</Product_price>
</Product>
<Product>
<Product_id>31</Product_id>
<Product_name>Product 31</Product_name>
<Product_price>13000</Product_price>
</Product>
<Product>
<Product_id>41</Product_id>
<Product_name>Product 41</Product_name>
<Product_price>14000</Product_price>
</Product>
</Product1>

</Table>


Source Code:





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Xml_ex
{
    public partial class Readxml : Form
    {
        public Readxml()
        {
            InitializeComponent();
        }

        private void Readxml_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList xmlnodes;
            XmlNode node;
            string strDocelement = "";
            int i = 0;
            string str = null;
            string strXmlLoc = "C:\\Users\\vamsi\\Desktop\\products.xml";
            xmldoc.Load(strXmlLoc);
            node = xmldoc.DocumentElement;
            if (node.HasChildNodes)
            {
                foreach (XmlNode xmlWrknode in node.ChildNodes)
                {
                    foreach (XmlNode xmlsubWrknode in xmlWrknode.ChildNodes)
                    {
                        foreach (XmlNode xmlsemisubWrknode in xmlsubWrknode.ChildNodes)
                        {

                            str = xmlsemisubWrknode.InnerText.ToString().Trim();
                            MessageBox.Show(str + "-");
                        }
                    }
                }
            }
           
        }
    }
}


No comments:

Post a Comment