Pages

Monday, October 1, 2018

How to read XML in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            // Start with XmlReader object

            //here, we try to setup Stream between the XML file nad xmlReader

            using (XmlReader reader = XmlReader.Create(@"D:\xmlFile\demo.xml"))

            {

                while (reader.Read())

                {

                    if (reader.IsStartElement())

                    {

                        //return only when you have START tag



                        switch (reader.Name.ToString())

                        {

                            case "Name":

                            Response.Write("Name of the Element is : " + reader.ReadString());

                                break;



                            case "Location":

                            Response.Write("Your Location is : " + reader.ReadString());

                                break;

                        }

                    }

               

                }

            }

         

        }

    }
---------------------------------XML file--------------------------
<?xml version="1.0" encoding="utf-8"?>

<Students>

  <Student>

    <Name>suneel</Name>

    <Location>Delhi</Location>

  </Student>

  <Student>

    <Name>Sadhna</Name>

    <Location>location4</Location>

  </Student>

  <Student>

    <Name>Name2</Name>

    <Location>location2</Location>

  </Student>

  <Student>

    <Name>Name3</Name>

    <Location>location3</Location>

  </Student>

  <Student>

    <Name>Name4</Name>

    <Location>location4</Location>

  </Student>

</Students>

https://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part
https://www.codeproject.com/Articles/487571/XML-Serialization-and-Deserialization-Part

No comments:

Post a Comment