0
    Dim root As XElement = XElement.Load(xmlFile)
    Dim stuff =
        From item In root.Elements("abc") Select item

    Debug.Print(stuff.Count)

and the contents of the XML are:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
 xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
  <abc xmlns="foo">1</abc>
  <abc xmlns="foo">2</abc>
  <abc xmlns="foo">3</abc>
</Workbook>

If I remove xmlns="urn:schemas-microsoft-com:office:spreadsheet" the beginning of the the Workbook tag, I get the correct result of 3

ETA What do I do if I have another namespace embedded, in this case "foo"

WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
  • 1
    The `abc` elements are in the namespace `urn:schemas-microsoft-com:office:spreadsheet` See here how to handle namespaces http://stackoverflow.com/questions/2340411/use-linq-to-xml-with-xml-namespaces – StuartLC Aug 16 '12 at 11:44
  • possible duplicate of [Parse xml elements and their attributes in LINQ](http://stackoverflow.com/questions/11052958/parse-xml-elements-and-their-attributes-in-linq) – sloth Aug 16 '12 at 11:45

1 Answers1

2

You're trying to find an element with a name abc without any namespace. The xmlns=... part of the parent element sets the default namespace for descendant elements.

You need:

Dim ns As XNamespace = "urn:schemas-microsoft-com:office:spreadsheet"
...
Dim stuff = root.Elements(ns + "abc")

Note that there's no point in using a query expression here - if you're just doing From x in y Select x you can just use y instead...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194