将 XDocument 与命名空间结合使用

发布于 2024-12-19 14:03:32 字数 2068 浏览 0 评论 0原文

我正在通过 XDocument 类读取 xml 文件。 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 节点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-12-26 14:03:32

使用 XNamespace 及其加法运算符重载的 LINQ to XML 非常简单:

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

命名空间的简单处理是 LINQ to XML 最美的方面之一。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文