locked
xml import to database RRS feed

  • Question

  • User330911564 posted

    Can i get a little help here please? I'm getting this error and the codes

    I trying to load my xml file into my database using entity framework and i'm kind of new in xml . 

    System.NullReferenceException: Object reference not set to an instance of an object. at XMLdb.index.<btnimport_click>b__1(XElement adStarCar) in c:\Users\Gabriel\Documents\Visual Studio 2013\Projects\XMLdb\XMLdb\index.aspx.cs:line 27 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at XMLdb.index.btnImport_Click(Object sender, EventArgs e) in c:\Users\Gabriel\Documents\Visual Studio 2013\Projects\XMLdb\XMLdb\index.aspx.cs:line 27

    the class that i created (.cs)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    using System.Xml.Serialization;
    
    namespace XMLdb
    {
        [Serializable]
        [XmlRoot("Cars")]
        public class CarImportMetaData
        {
    
            [XmlElement("unqNumber")]
            public int unqNumber { get; set; }
    
            [XmlElement("lasMute")]
            public Nullable<System.DateTime> lasMute { get; set; }
    
            [XmlElement("name")]
            public string name { get; set; }
    
            [XmlElement("email")]
            public string email { get; set; }
    
            [XmlElement("carNumber")]
            public string carNumber { get; set; }
    
            [XmlElement("company")]
            public string company { get; set; }
    
            [XmlElement("model")]
            public string model { get; set; }
    
            [XmlElement("imgBig")]
            public string imgBig { get; set; }
        }
    
    
        [MetadataType(typeof(CarImportMetaData))]
        public partial class StarCarXml
        {
    
        }
    }

    and when button is click after uploading the xml file

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml.Linq;
    
    namespace XMLdb
    {
        public partial class index : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnImport_Click(object sender, EventArgs e)
            {
                try
                {
                    if (this.FileUploadXMLFile.PostedFile.ContentType.Equals("application/xml") || this.FileUploadXMLFile.PostedFile.ContentType.Equals("text/xml"))
                    {
                        var xmlPath = Server.MapPath("~/Content/" + this.FileUploadXMLFile.FileName);
                        this.FileUploadXMLFile.SaveAs(xmlPath);
                        XDocument xDoc = XDocument.Load(xmlPath);
                        List<StarCarXml> starCarList = xDoc.Descendants("adStarCar").Select(adStarCar => new StarCarXml
                        {
                            unqNumber = Convert.ToInt32(adStarCar.Element("unqNumber").Value),
                            lasMute = Convert.ToDateTime(adStarCar.Element("lasMute").Value),
                            name = adStarCar.Element("name").Value,
                            email = adStarCar.Element("email").Value,
                            carNumber = adStarCar.Element("carNumber").Value,
                            company = adStarCar.Element("company").Value,
                            model = adStarCar.Element("model").Value,
                            imgBig = adStarCar.Element("imgBig").Value
                        }).ToList();
    
                        using (xmlDBEntities db = new xmlDBEntities())
                        {
                            foreach (var i in starCarList)
                            {
                                var v = db.StarCarXmls.Where(a => a.unqNumber.Equals(i.unqNumber)).FirstOrDefault();
                                if (v != null)
                                {
                                    v.unqNumber = i.unqNumber;
                                    v.lasMute = i.lasMute;
                                    v.name = i.name;
                                    v.email = i.email;
                                    v.carNumber = i.carNumber;
                                    v.company = i.company;
                                    v.model = i.model;
                                    v.imgBig = i.imgBig;
                                }
                                else
                                {
                                    db.StarCarXmls.Add(i);
                                }
                            }
                            db.SaveChanges();
                            Response.Redirect("success.aspx");
                        }
                    }
                }
                catch(Exception ex)
                {
                    lblError.Text = ex.ToString();
                }
            }
        }
    }

    and this is the xml file

    <?xml version="1.0" encoding="utf-8" ?>
    <Cars>
      <adStarCar>
        <unqNumber>23260</unqNumber>
        <lasMute>2017-02-28</lasMute>
        <DealSafer>
          <name>Gabe T</name>
          <email>gabriel@gmail.com</email>
        </DealSafer>
        <car>
          <carNumber>87-98-AE</carNumber>
          <company>Audi</company>
          <model>A8</model>
        </car>
        <image>
          <imgBig>http://srv2.betterparts.org/images/audi-a8-01.jpg</imgBig>
        </image>
      </adStarCar>
      <adStarCar>
        <unqNumber>8955</unqNumber>
        <lasMute>2017-01-13</lasMute>
        <DealSafer>
          <name>Richard A</name>
          <email>richard@gmail.com</email>
        </DealSafer>
        <car>
          <carNumber>78-KL-85</carNumber>
          <company>Mercedes</company>
          <model>Maybach</model>
        </car>
        <image>
          <imgBig>http://zombdrive.com/images/maybach-11.jpg</imgBig>
        </image>
      </adStarCar>
    </Cars>
    

    Thursday, March 2, 2017 9:33 AM

Answers

  • User1771544211 posted

    Hi tandohtakyie,

    thank you for your help but i'm still getting the same error. (Object reference not set to an instance of an object)

    The Lambda statement is hard to debug, please use the following code to check if all the values are not empty.

    var test = xDoc.Descendants("adStarCar");
    foreach (var test2 in test)
    {
    
        int unqNumber = Convert.ToInt32(test2.Element("unqNumber").Value);
        string name = test2.Element("DealSafer").Element("name").Value;
        string email = test2.Element("DealSafer").Element("email").Value;
        string carNumber = test2.Element("car").Element("carNumber").Value;
        string company = test2.Element("car").Element("company").Value;
        string model = test2.Element("car").Element("model").Value;
        string imgBig = test2.Element("image").Element("imgBig").Value;
    
    };

    It's more easier to debug the values of each field.

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 3, 2017 8:55 AM

All replies

  • User-359936451 posted

    Not sure but it looks like you are trying to move past the end of the XML file...

    Try changing this

    foreach (var i in starCarList)

    to this

    foreach (var i in starCarList - 1)

    I would check the length of the file, and set some break points to see of you are processing through to the end or is it crashing before it even processes a record. Show us which line the code crashes on.

    Thursday, March 2, 2017 3:06 PM
  • User1771544211 posted

    Hi tandohtakyie,

    name = adStarCar.Element("name").Value,

    The name element is the child element of DealSafer in your xml file. So you need first get the DealSafer element, then to get the name element value.

    You can try the following code, which works fine on my side:

    List<StarCarXml> starCarList = xDoc.Descendants("adStarCar").Select(adStarCar => new StarCarXml()
    {
        unqNumber = Convert.ToInt32(adStarCar.Element("unqNumber").Value),
        lasMute = Convert.ToDateTime(adStarCar.Element("lasMute").Value),
        name = adStarCar.Element("DealSafer").Element("name").Value,
        email = adStarCar.Element("DealSafer").Element("email").Value,
        carNumber = adStarCar.Element("car").Element("carNumber").Value,
        company = adStarCar.Element("car").Element("company").Value,
        model = adStarCar.Element("car").Element("model").Value,
        imgBig = adStarCar.Element("image").Element("imgBig").Value
    
    }).ToList();

    Best Regards,

    Jean

    Friday, March 3, 2017 6:06 AM
  • User330911564 posted

    Hi Jean,

    thank you for your help but i'm still getting the same error. (Object reference not set to an instance of an object)

    Friday, March 3, 2017 8:12 AM
  • User1771544211 posted

    Hi tandohtakyie,

    thank you for your help but i'm still getting the same error. (Object reference not set to an instance of an object)

    The Lambda statement is hard to debug, please use the following code to check if all the values are not empty.

    var test = xDoc.Descendants("adStarCar");
    foreach (var test2 in test)
    {
    
        int unqNumber = Convert.ToInt32(test2.Element("unqNumber").Value);
        string name = test2.Element("DealSafer").Element("name").Value;
        string email = test2.Element("DealSafer").Element("email").Value;
        string carNumber = test2.Element("car").Element("carNumber").Value;
        string company = test2.Element("car").Element("company").Value;
        string model = test2.Element("car").Element("model").Value;
        string imgBig = test2.Element("image").Element("imgBig").Value;
    
    };

    It's more easier to debug the values of each field.

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 3, 2017 8:55 AM