我正在通过XDocument
课阅读xmlfile。 XML中有名称空间,如何将node.Element()
与名称空间一起使用?我尝试创建XNamespace
,但似乎没有效果......
XML如下:
<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>
我正在从foreach
中的节点进行循环,并希望获得s:Song
节点。
答案 0 :(得分:3)
使用XNamespace进行LINQ to XML以及加法运算符的重载非常简单:
XNamespace ns = "urn:schemas-rcsworks-com:SongSchema";
for (var songElement = doc.Descendants(ns + "Song"))
{
...
}
命名空间的简单处理是LINQ to XML最美丽的方面之一。