Unable to Select in LINQ to XML, error: Object reference not set to an instance of an object -
I'm new to using Linq for XML and I have to go through a troublesome error. When trying to drag in my XML file, I get an error that "object reference is not set to an instance of an object." And it is saying that this error is because I am trying to choose a new statement. I've attached my code below:
using the system; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Xml.Linq; Public Partial Class _Default: System.Web.UI.Page {Secure Zero Page_load (Object Sender, EventArgs E) {XDocument feed = XDocument.Load (Server.MapPath ("VEHICLES.XML")); Var query = c to feed.Descendants ("vehicle") where (string) c.Element ("VehicleType"). Select value == "0" new {Vin = c.Element ("Vin"). Value, condition = c. Element ("vehicle type"). Value, year = c. Element ("year"). Values}; CarLister.DataSource = Query; CarLister.DataBind (); }} Selects the code to pull properly in all nodes; Instead of selection, but ideally I would like to select only certain information and there are some entries that I have to modify while dragging in. I am not really sure what is the reason for this issue, so any indication or thought will be very appreciated for how to fix this, just ask if you need any other information!
The problem is that you are using the value property everywhere - which fails when empty due to not being the result of foo.Element (...) . Fortunately, this is an easy way - rather instead. The XElement tends to clear string if "input" is zero, which is very simple. from var query = c ("vehicle") where (string) c.Element ("VehicleType") == "0" new {Vin = (string) c.Element ("Vin "), Select position = (string) c. Element ("vehicle type"), year = (string) c.Element ("year")}; This will be saved in a case that is a & lt; Vehicles & gt; element in & lt; VehicleType & gt; , & lt; Vin & gt; , & lt; Status & gt; or & lt; Year & gt; Children However, if this failure actually means it was invalid to start with the data, you might want an exception anyway. It depends on how much you depend on the data to be smart to get started.
Comments
Post a Comment