I'm having real trouble with a join that I am trying to perform on two separate XML files.
I have two classes that describe each object that I extract from the XML files, they are
public class XMLCustomers{ public String CustomerID { get; set; } public String ContactName { get; set; }}
and
public class XMLOrders{ public String OrderID { get; set; } public String CustomerID { get; set; } public String OrderDate { get; set; } public String ShippedDate { get; set; } public String Freight { get; set; } public String ShipName { get; set; } public String ShipCity { get; set; } public String ShipCountry { get; set; }}
And my last class stores the join that I perform on the two sets of data.
public class PostXMLJoinOrder{ public String OrderID { get; set;} public String ContactName { get; set;} public String OrderDate { get; set;} public String ShippedDate { get; set;} public String Freight { get; set;} public String ShipName { get; set;} public String ShipCity { get; set;} public String ShipCountry { get; set;}}
Finally these are my two methods that load the information from the XML files and the third method performs a join and stores the information in a IEnumerable
public IEnumerable<XMLCustomers> LoadXMLCustomers() { var custs = from x in XElement.Load(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\XCustomers.xml")).Elements() select new XMLCustomers { ContactName = x.Attribute("ContactName").Value, CustomerID = x.Attribute("CustomerID").Value }; int size = custs.Count(); return custs; } public IEnumerable<XMLOrders> LoadXMLOrders() { var ords = from x in XElement.Load(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\XOrders.xml")).Elements() select new XMLOrders { OrderID = x.Attribute("ContactName").Value, CustomerID = x.Attribute("CustomerID").Value, OrderDate = x.Attribute("OrderDate").Value, ShippedDate = x.Attribute("ShippedDate").Value, Freight = x.Attribute("Freight").Value, ShipName = x.Attribute("ShipName").Value, ShipCity = x.Attribute("ShipCity").Value, ShipCountry = x.Attribute("ShipCountry").Value }; int size = ords.Count(); return ords; } public IEnumerable<PostXMLJoinOrder> LoadPostXMLJoinOrders() { var joinQuery = from customer in LoadXMLCustomers() from orders in LoadXMLOrders() where customer.CustomerID == orders.CustomerID select new PostXMLJoinOrder { OrderID = orders.OrderID, ContactName = customer.ContactName, OrderDate = orders.OrderDate, ShippedDate = orders.ShippedDate, Freight = orders.Freight, ShipName = orders.ShipName, ShipCity = orders.ShipCity, ShipCountry = orders.ShipCountry }; return joinQuery; }
I have tested the amount of items returned from my LINQ and it continues to be 0;I have double checked everything being loaded in from the files but I cant seem to get my head around at which point its going wrong.
Edit:its a loading issue. the XML files are definitely stored under the correct App_Data folder. But when the individial LoadXMLCustomers() is running Im getting a NullReferenceException at the part where the lINQ statement selects and creates the new Loaded Customer object.
I have already made sure the build for the XML documents are content and copyToOutputDirectory is set to if newer
this is the Exception & Also the var value is null so its definitely not loading for some reason:Image may be NSFW.
Clik here to view.
SOLVED: The lesson I learnt here is that you need to pay close attention to your XML and data. If some of your XML data has empty values, then you need to account by making sure the select statement can handle it.
Above I had the code
select new XMLOrders { OrderID = x.Attribute("ContactName").Value, CustomerID = x.Attribute("CustomerID").Value, OrderDate = x.Attribute("OrderDate").Value, ShippedDate = x.Attribute("ShippedDate").Value, Freight = x.Attribute("Freight").Value, ShipName = x.Attribute("ShipName").Value, ShipCity = x.Attribute("ShipCity").Value, ShipCountry = x.Attribute("ShipCountry").Value };
Which really should have included casts to string values so if there is empty data is "" not throwing a null exception.Secondly I should have been getting the Element values not the Attribute values.
select new XMLOrders { OrderID = (string)x.Element("OrderID").Value, CustomerID = (string)x.Element("CustomerID").Value, OrderDate = (string)x.Element("OrderDate").Value, ShippedDate = (string)x.Element("ShippedDate").Value, Freight = (string)x.Element("Freight").Value, ShipName = (string)x.Element("ShipName").Value, ShipCity = (string)x.Element("ShipCity").Value, ShipCountry = (string)x.Element("ShipCountry").Value };