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

    Error showing while accessing xml file

    Hi,

    I want to read and display an xml in Gridview

    This is my XML file

    <?xml version="1.0" encoding="utf-8"?>
    <TourDetails>
    <Tour
    PCode="DEL"
    TransCode="TSC1000"
    Name="Transfer Delhi to Agra enroute Mathura"
    From="Delhi"
    Vehicle="Large Coach AC"
    Rate="15000" />
    <Tour
    PCode="DEL"
    TransCode="TSC1000"
    Name="Transfer Delhi to Agra enroute Mathura"
    From="Delhi"
    Vehicle="Large Coach NAC"
    Rate="10000" />
    <Tour
    PCode="DEL"
    TransCode="TSC1000"
    Name="Transfer Delhi to Agra enroute Mathura"
    From="Delhi"
    Vehicle="Ambassador AC"
    Rate="8000" />
    </TourDetails>

    and i have tried following code

    XDocument xmlDoc;

    xmlDoc = XDocument.Load(Server.MapPath("Tour.xml"));
    var fee = from p in xmlDoc.Descendants("TourDetails")

    select new
    {

    Pcode = p.Element("PCode").Value,
    TransCode = p.Element("TransCode").Value,
    Name = p.Element("Name").Value,
    };

    GridView1.DataSource = fee;
    GridView1.DataBind();

    It showing error

    Object reference not set to an instance of an object.

    How to solve this

    Regards

    Baiju
  • #763538
    which line shows error ?
    if you want to bind xml to gridview then first you need to read that xm to dataset and then check for dataset columns and then bind it to gridview
    see below snippet
    using (DataSet ds = new DataSet())
    {
    ds.ReadXml(Server.MapPath("~/Customers.xml"));
    GridView1.DataSource = ds;
    GridView1.DataBind();
    }
    Here when you load xml in dataset then check if required data is properly loaded in application

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

  • #763541
    Before assign the values check the null/Instance created.

    In your case in which line you are getting exception?

    By Nathan
    Direction is important than speed

  • #763545
    Hi,

    As per error details "Object reference not set to an instance of an object" when you are trying to access null value in that case you may get this type of errors to resolve this, check the null case before access the object.

    And coming back to your thread if you want to read XML file and display it in gridview there is a method called READXML, by using this method you can able to read the xml in to datasource and using that source you can display it in datasource control like gridview.


    ds.ReadXml("xml file);
    gv.DataSource=ds;
    gv.DataBind();

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

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

  • #766190
    Hi
    If structure of xml is proper . then you can directly read xml into dataset and Dataset having ReadXml method
    after that you can bind datatable of dataset to the datasource of gridview.

    Thanks
    Umesh Bhosale


Sign In to post your comments