You must Sign In to post a response.
  • Category: Windows

    How to Read records of XML file into gridview ?


    Are you looking for a way to Read records of XML file into gridview ? then read this thread to know how to do it ?



    Hi,

    I am using Windows Application and Access

    I inserted records of gridview in XML file but I am not able to read record ID wise back into gridview.

    I want to insert records related to ID back into gridview.I need code for that.
  • #735943
    Hi,

    Right Clickon your project and Choose Add new Item and then choose XML File option then give a name of that file as Employee.XML. Then design the XML file like below.


    <?xml version="1.0" encoding="utf-8" ?>
    <EmployeeDetails>
    <Employee>
    <EmployeeNumber>100</EmployeeNumber>
    <EmployeeName>Naveen</EmployeeName>
    <Job>.NET Develper</Job>
    <Manager>Siva S.K</Manager>
    <Salary>10000</Salary>
    </Employee>
    <Employee>
    <EmployeeNumber>101</EmployeeNumber>
    <EmployeeName>Sekhar</EmployeeName>
    <Job>SharePoint Administrator</Job>
    <Manager>Siva S.K</Manager>
    <Salary>10000</Salary>
    </Employee>
    </EmployeeDetails>

    and in code behind wrote below lines of code.


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

    public partial class XML : System.Web.UI.Page
    {
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
    ds = new DataSet();
    ds.ReadXml(MapPath("~/Employee.xml"));
    GV.DataSource = ds;
    GV.DataBind();
    }
    }





    Refer below link for more information

    http://www.dotnetspider.com/resources/45122-XML-DataSource-ASP.net.aspx

    Try something like above to achieve your goal.

    Hope this will help you to resolve the issue..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #735944
    You need to take help of dataset for it. There is no need to read xml file tag wise and insert in gridview, with the help of dataset we can directly insert XML to dataset and then bidn the dataset to gridview
    see below code snippet

    Dataset objDS = new Dataset();
    objDS.ReadXml("D:\\test.xml");
    gridview1.Datasource = objDS;
    gridview1.DataBind();

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #735950
    You have to verify that the XML you are creating is in corect format so that you can read the values as per IDs present in the record.

    here is a sample code to read XML element by element.


    sing System;
    using System.Xml;

    namespace ReadXML
    {
    class XMLRead
    {
    static void Main(string[] args)
    {
    XmlTextReader reader = new XmlTextReader ("Anil.xml");
    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element:
    Console.Write("Element: " + reader.Name);
    Console.WriteLine(">");
    break;
    case XmlNodeType.Text:
    Console.WriteLine ("Value:" + reader.Value);
    break;
    case XmlNodeType.EndElement:
    Console.Write("Last Element" + reader.Name);
    break;
    }
    }
    Console.ReadLine();
    }
    }
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #735958
    [Response removed by Admin. Read forum policies.]

  • #735960
    see below code:

    private void butReadXml_Click(object sender, EventArgs e)
    {
    DataSet ds = new DataSet();
    ds.ReadXml("d:\\Employee_onlyxml.xml");
    dataGridViewShowXml.DataSource = ds;
    dataGridViewShowXml.DataMember = "Employee";
    }

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..

  • #736044
    Hi,

    Please check below code and modify according to your requirement,


    Code to read XML file,



    Private Sub PopulateXMLData(Empid As String)
    Try
    Dim XMLFilePath As String = System.Configuration.ConfigurationManager.AppSettings("XMLFileDetails")
    'add an entry in web.coinfig for XML file path.
    Dim ds As New DataSet()
    ds.ReadXml(XMLFilePath)

    For Each dr As DataRow In ds.Tables(0).Rows
    If (dr("Employeeid").ToString()) = Empid.ToString() Then
    txtName.Text = dr("Name").ToString().Trim()
    txtAddress.Text = dr("Address").ToString().Trim()
    'Continue for all fields....
    txtPhone.Text = dr("Phone").ToString().Trim()
    End If
    Next
    Catch ex As Exception
    lblMsg.Text = "Error while fetching the record" + ex.Message
    End Try
    End Sub


    Code to write XML file

    Private Sub WriteXML()
    Try
    Dim XMLFilePath As String = System.Configuration.ConfigurationManager.AppSettings("XMLFileDeatils")
    Dim ds As New DataSet()
    ds.ReadXml(XMLFilePath)


    Dim NewRow As DataRow = ds.Tables(0).NewRow()
    NewRow("Id") = txtId.Text
    NewRow("Name") = txtName.Text


    ds.Tables(0).Rows.Add(NewRow)
    ds.WriteXml(XMLFilePath)
    Catch ex As Exception
    Label1.Text = "Error"
    End Try
    End Sub


    Regards,
    Asheej T K

  • #736095
    Hi,

    Dataset ds =new dataset();
    ds.ReadXml(path of the xml file);
    gv.Datasource =ds
    gv.DataBind()

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI


  • Sign In to post your comments