paulinesienna
New Member
I have already asked a simular question, granted, however, this one is different and I have tried.I was attempting to read XML data using Linq and I sorted this problem out, however, I need to select multiple values and store these into a Dictionary. I have tried (and the comments are my attempts) but I just cannot figure it out and get an error like:\[quote\] System.NullReferenceException: Object reference not set to an instance of an object at dsdsdsds.MainClass+c_AnonStorey0.<>m_0 (System.Xml.Linq.XElement datanum) [0x00000] in /Users/p/Projects/dsdsdsds/dsdsdsds/Main.cs:23 \[/quote\]Let's assume my XML file looks like this:\[code\]<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>My word place</name> <Placemark> <name>Main Building</name> <id>1</id> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates>\[/code\]Now what I need to get is the "name" and the "id" and put these values into a dictionary. Here is my code:\[code\]var xdoc = XDocument.Load ("buildings.kml"); XNamespace kml = "http://www.opengis.net/kml/2.2"; var dict = xdoc.Descendants(kml + "Placemark").ToDictionary ( datanum => datanum.Attribute(kml + "name").Value, datanum => datanum.Attribute(kml + "id").Value ); /*.ToDictionary(e => e.Element("name").Value, e => e.Descendants("id") .Select (item => new Building { BuildingName = (string) e.Element(kml + "name").Value, BuildingID = Convert.ToInt16(item.Element(kml + "id").Value) }).ToList() ); /* /*var buildings = xdoc.Descendants (kml + "Placemark") .Select((element, index) => new Building { BuildingName = (string) element.Element(kml + "name"), BuildingID = (int) element.Element(kml + "id") }); */\[/code\]Note: I have made three attempts to do this, however, getting the same / simular error messages. -- I am also learning LINQ and Dictionaries. Please bare with me Hope someone can help.