1

I am reading an xmlfile via the XDocument class. The XML has namespaces in it, how do I use node.Element() with a namespace? I tried creating XNamespace but it didn't seem to work..

The XML is as follows:

<CueList time="2011-12-02T13:34:39" xmlns="urn:CueListSchema.xml" xmlns:s="urn:schemas-rcsworks-com:SongSchema" xmlns:n="urn:schemas-rcsworks-com:NoteSchema" xmlns:l="urn:schemas-rcsworks-com:LinkSchema" xmlns:t="urn:schemas-rcsworks-com:TrafficSchema" xmlns:p="urn:schemas-rcsworks-com:ProductSchema" xmlns:m="urn:schemas-rcsworks-com:MediaSchema" xmlns:w="urn:schemas-rcsworks-com:WebPageSchema" xmlns:ns="urn:CueListSchema.xml">
  <Event eventID="14" eventType="song" status="happening" scheduledTime="13:34:38" scheduledDuration="332.82">
    <s:Song title="Long Time Coming (Holding On)" internalID="007700028007AD480000">
      <s:Artist name="The Winans" sequenceNumber="1" internalID="0067000180002B020000" sortName="Winans, The" />
      <m:Media ID="{7C734B6C-7AF7-4998-8366-F1F11F5D56D7}" runTime="332.82" fileName="{7C734B6C-7AF7-4998-8366-F1F11F5D56D7}.wav" />
    </s:Song>
  </Event>
  <Event eventID="15" eventType="link" status="committed" startTime="13:40:10" scheduledDuration="3.49">
    <l:Link title="PG MFL DRY FEMALE" internalID="007B00028002DAEA0000">
      <m:Media ID="{036BB0ED-3130-4AD0-8BAF-E5D0FBA7DC3B}" runTime="3.49" fileName="{036BB0ED-3130-4AD0-8BAF-E5D0FBA7DC3B}.wav" />
    </l:Link>
  </Event>
  <Event eventID="16" eventType="song" status="committed" startTime="13:40:10" scheduledDuration="303.55">
    <s:Song title="Not Making Sense, Making Faith" internalID="007700028009377F0000">
      <s:Artist name="Donald Lawrence" sequenceNumber="1" internalID="006700018000308A0000" sortName="Lawrence, Donald" />
      <m:Media ID="{B6FD04EA-9B42-4E6A-AC80-A26BF65E6F11}" runTime="303.55" fileName="{B6FD04EA-9B42-4E6A-AC80-A26BF65E6F11}.wav" />
    </s:Song>
  </Event>
</CueList>

I am looping from the nodes in a foreach and want to get the s:Song node.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Luke Wilkinson
  • 439
  • 8
  • 17

1 Answers1

3

Nice and easy with LINQ to XML using XNamespace and its overload of the addition operator:

XNamespace ns = "urn:schemas-rcsworks-com:SongSchema";
for (var songElement = doc.Descendants(ns + "Song"))
{
    ...
}

The simple handling of namespaces is one of the most beautiful aspects of LINQ to XML.

Martin
  • 173
  • 3
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks I can see that this should work, is it possible to use node.Element(ns + "song") - because when I do it errors... – Luke Wilkinson Dec 02 '11 at 15:13
  • @LukeWilkinson: Yes, that should be fine. What do you mean by "it errors"? Please be specific. – Jon Skeet Dec 02 '11 at 15:29
  • sorry - Object reference not set to an instance of an object. – Luke Wilkinson Dec 02 '11 at 15:33
  • @LukeWilkinson: That would suggest that `node` is null. – Jon Skeet Dec 02 '11 at 15:36
  • "one of the most beautiful aspects of LINQ to XML." Are you serious ? Personally I find all of this totally crappy – Gab Jul 24 '13 at 11:22
  • @Gab: I'm absolutely serious. LINQ to XML is the nicest XML API I've ever used. How would you have designed it? – Jon Skeet Jul 24 '13 at 12:07
  • Design is not bad but seriously why the hell does it propagate namespace across all dom elements ? If i need the ns I know where to find it. JDom would be my reference, but it's not in C# – Gab Jul 24 '13 at 12:31
  • @Gab: It does that because that's what the XML model is - the name of an element is *not* just the local name, it's the local name and namespace. I *far* prefer LINQ to XML over JDom. – Jon Skeet Jul 24 '13 at 12:33
  • Well, when you have only one default namespace just here to resolve the associated schema, i see no reason of propagating it. You're probably right that remaining part of API is much more powerful than classical DOM. If i never liked something in C# it is clearly the linq approach. – Gab Jul 24 '13 at 12:54