Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am learner in Programming I am making a project where, I am making a took which will read the text between the particular tags of XML file.

Its really small example i am sowing here.
Ex.

XML
<?xml version="1.0"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>


Now here, Suppose i want to read the text between the tag <heading> and want to export this text in Excel file.

So can you please help me out how can it will be possible.

Thanks and Regards,
Mayur Alaspure
Posted

Dear Friend,

Here what you can do is:-

Use System.XML namespace DOM classes getElementsByTagName method.

.....
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(@"c:\csnet\46\CSonsole\a1.xml");
System.Xml.XmlElement root = doc.DocumentElement;
System.Xml.XmlNodeList lst = root.GetElementsByTagName("id");

foreach (System.Xml.XmlNode n in lst)
{
Console.WriteLine(n.InnerText); // print id text
}


Please refer the links :- http://csharp.net-informations.com/xml/how-to-read-xml.htm[^]

http://www.daniweb.com/software-development/csharp/threads/195928[^]
 
Share this answer
 
v2
Comments
Varun Sareen 21-Feb-12 6:41am    
[Solution 3: added as comment]
Thanks a lot Varun Sareen Your answer helps me Lot... :)

By: Mayur2258
Varun Sareen 21-Feb-12 6:42am    
Please mark it as your answer.
Try this :)
C#
using System;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Cache["Motos"] == null)
        {
            ds.ReadXml(Server.MapPath("~/XMLFiles/Motors.xml"), XmlReadMode.InferSchema);
            Cache.Insert("Motos", ds);
        }
        else
            ds = (DataSet)Cache["Motos"];
    }

    protected void drpMain_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1200);

        if (drpMain.SelectedValue == "0")
        {
            drpMoto.Items.Clear();
            drpBrand.Items.Clear();
            drpMoto.Enabled = false;
            drpBrand.Enabled = false;
            btnGo.Enabled = false;
        }
        else if (drpMain.SelectedValue == "1")
        {
            drpMoto.DataTextField = "mcname";
            drpMoto.DataValueField = "mcid";
            drpMoto.DataSource = ds.Tables["motoc"];
            drpMoto.DataBind();
            drpMoto.Enabled = true;
            drpBrand.Items.Clear();
            drpBrand.Enabled = false;
            btnGo.Enabled = false;
        }
        else if (drpMain.SelectedValue == "2")
        {
            drpMoto.DataTextField = "mbname";
            drpMoto.DataValueField = "mbid";
            drpMoto.DataSource = ds.Tables["motob"];
            drpMoto.DataBind();
            drpMoto.Enabled = true;
            drpBrand.Items.Clear();
            drpBrand.Enabled = false;
            btnGo.Enabled = false;
        }
    }

    protected void drpMoto_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1200);

        if (drpMoto.SelectedItem.Value == "0")
        {
            drpBrand.Items.Clear();
            drpBrand.Enabled = false;
            btnGo.Enabled = false;
        }
        else if (drpMain.SelectedValue == "1" && drpMoto.SelectedItem.Value != "0")
        {
            DataView dView = new DataView(ds.Tables["motocb"]);
            dView.RowFilter = "mcid='" + drpMoto.SelectedValue.Trim() + "'";
            dView.Sort = "mcbname asc";
            drpBrand.DataTextField = "mcbname";
            drpBrand.DataValueField = "mcbid";
            drpBrand.DataSource = dView;
            drpBrand.DataBind();
            drpBrand.Enabled = true;
            btnGo.Enabled = true;
        }
        else if (drpMain.SelectedValue == "2" && drpMoto.SelectedItem.Value != "0")
        {
            DataView dView = new DataView(ds.Tables["motobb"]);
            dView.RowFilter = "mbid='" + drpMoto.SelectedValue.Trim() + "'";
            dView.Sort = "mbbname asc";
            drpBrand.DataTextField = "mbbname";
            drpBrand.DataValueField = "mbbid";
            drpBrand.DataSource = dView;
            drpBrand.DataBind();
            drpBrand.Enabled = true;
            btnGo.Enabled = true;
        }
    }

    protected void btnGo_OnClick(object sender, EventArgs e)
    {
        lblResult.Text = "Searched for: " + drpBrand.SelectedItem.Text.Replace("(","").Replace(")","") + " " + drpMoto.SelectedItem.Text + " " + drpMain.SelectedItem.Text.Replace("s","") + "(s)";
    }
}
 
Share this answer
 
Comments
BobJanova 21-Feb-12 6:58am    
What's this? Just a code dump of a semi-related web page? This doesn't answer the question at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900