Selecting elements from XML file using LINQ

26,923

Solution 1

You forgot about namespace declared in your xml:

var xdoc = XDocument.Load("buildings.kml");
XNamespace kml = "http://www.opengis.net/kml/2.2";
var buildings = xdoc.Root.Elements(kml + "Document")
                    .Select(d => new Building {
                        BuildingName = (string)d.Element(kml + "name")
                    }).ToList();

Solution 2

XDocument xDocument = XDocument.Load("buildings.kml");
XNamespace xNameSpace = "http://www.opengis.net/kml/2.2";

var names = from o in xDocument.Descendants(xNameSpace+"name")
            select o.Value;

I think this is the easiest way; do not forget to add namespace before the queried element.

Solution 3

From what I can see, you're attempting to loop over the "Document" -elements and select their names. Instead, you probably want to go one step further, into the Placemark -elements, ie.

XDocument.Load("buildings.kml").Element("Document").Elements("Placemark")
                    select new Building
                    {
                        BuildingName = e.Element("name").Value
                    }).ToList();
Share:
26,923
Phorce
Author by

Phorce

I mainly do Signal Processing and I code in languages such as: C++, Java, PHP. I also do Raspberry Pi development

Updated on July 09, 2022

Comments

  • Phorce
    Phorce almost 2 years

    I have this XML structure:

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
      <Document>
        <name>My Work</name>
        <Placemark>
          <name>Main Building</name>
          <Polygon>
            <extrude>1</extrude>
            <altitudeMode>relativeToGround</altitudeMode>
            <outerBoundaryIs>
              <LinearRing>
                <coordinates>
                </coordinates>
              </LinearRing>
            </outerBoundaryIs>
          </Polygon>
        </Placemark>
    
        <Placemark>
          <name>Office 1</name>
          <Polygon>
            <extrude>1</extrude>
            <altitudeMode>relativeToGround</altitudeMode>
            <outerBoundaryIs>
              <LinearRing>
                <coordinates>
                </coordinates>
              </LinearRing>
            </outerBoundaryIs>
          </Polygon>
        </Placemark>
      </Document>
    </kml>
    

    This goes on...

    I need to select the building "name" for each of them and store this inside a list. I have written this code:

    using System;
    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    namespace dsdsdsds
    {
        public class Building
        {
            public string BuildingName { get; set; }
        }
    
        class MainClass
        {
            public static void Main(string[] args)
            {
                List<Building> buildingNames = 
                    (from e in XDocument.Load("buildings.kml").Root
                                        .Elements("Document")
                     select new Building
                     {
                         BuildingName = (string)e.Element("name")
                     }).ToList();
    
                foreach (var e in buildingNames)
                {
                    Console.WriteLine(e);
                }
            }
        }
    }
    

    However, it doesn't seem to want to output anything and I cannot find out where I am going wrong. Could anyone help me?

    Thanks