如何使用 XDocument 读取多个嵌入元素

发布于 2024-11-19 09:33:43 字数 3176 浏览 4 评论 0原文

以下是我正在阅读的 XML 文件的一部分:

<?xml version="1.0"?>
<movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ThumbGen="1">
  <hasrighttoleftdirection>false</hasrighttoleftdirection>
  <title>A Nightmare on Elm Street</title>
  <originaltitle>A Nightmare on Elm Street</originaltitle>
  <year>1984</year>
  <plot>Years after being burned alive by a mob of angry parents, child murderer Freddy Krueger returns to haunt the dreams -- and reality -- of local teenagers. As the town's teens begin dropping like flies, Nancy and her boyfriend, Glen, devise a plan to lure the monster out of the realm of nightmares and into the real world.</plot>
  <tagline>A scream that wakes you up, might be your own...</tagline>
  <metascore>78</metascore>
  <trailer>http://www.youtube.com/watch?v=996</trailer>
  <rating>8.6</rating>
  <episodes />
  <episodesnames />
  <writers />
  <gueststars />
  <id>tt0087800</id>
  <releasedate>11.09.1984</releasedate>
  <actor>
    <name>Robert Englund</name>
    <name>Heather Langenkamp</name>
    <name>Johnny Depp</name>
    <name>Ronee Blakley</name>
    <name>John Saxon</name>
    <name>Amanda Wyss</name>
    <name>Jsu Garcia</name>
    <name>Charles Fleischer</name>
    <name>Joseph Whipp</name>
    <name>Lin Shaye</name>
    <name>Joe Unger</name>
    <name>Mimi Craven</name>
    <name>David Andrews</name>
  </actor>
  <genre>
    <name>Horror</name>
    <name>Comedy</name>
  </genre>
  <director>
    <name>Wes Craven</name>
  </director>
  <runtime>91</runtime>
  <certification>R</certification>
  <studio>
    <name>New Line Cinema</name>
  </studio>
  <country>
    <name>United States of America</name>
  </country>
  ...
  ...
  ...
</movie>

感谢 Henk Holterman,我能够清理处理 XML 的代码,我现在正在使用以下内容:

var doc = XDocument.Load(n);  // takes care of all Open/Close issues
strTitle = doc.Root.Element("title") == null ? "" : doc.Root.Element("title").Value;
strYear = doc.Root.Element("year") == null ? "" : doc.Root.Element("year").Value;
strPlot = doc.Root.Element("plot") == null ? "" : doc.Root.Element("plot").Value;
strRating = doc.Root.Element("rating") == null ? "" : doc.Root.Element("rating").Value;
strMPAA = doc.Root.Element("mpaa") == null ? "" : doc.Root.Element("mpaa").Value;
strCertification = doc.Root.Element("certification") == null ? "" : doc.Root.Element("certification").Value;

现在最后一点,我怎样才能使用此方法从流派元素获取流派?我无法搜索名称 Element,因为它已在其他各种元素中使用。我不确定我是否可以使用:

doc.Root.Element("genre").ElementsAfterSelf("name");

不清楚返回什么(我不太明白如何使用 IEnumberables),或者它如何处理多个“名称”。我认为可以使用 LINQ 来完成,但我仍在尝试找出如何使用它。

非常感谢!!

Here is a portion of the XML file I'm reading:

<?xml version="1.0"?>
<movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ThumbGen="1">
  <hasrighttoleftdirection>false</hasrighttoleftdirection>
  <title>A Nightmare on Elm Street</title>
  <originaltitle>A Nightmare on Elm Street</originaltitle>
  <year>1984</year>
  <plot>Years after being burned alive by a mob of angry parents, child murderer Freddy Krueger returns to haunt the dreams -- and reality -- of local teenagers. As the town's teens begin dropping like flies, Nancy and her boyfriend, Glen, devise a plan to lure the monster out of the realm of nightmares and into the real world.</plot>
  <tagline>A scream that wakes you up, might be your own...</tagline>
  <metascore>78</metascore>
  <trailer>http://www.youtube.com/watch?v=996</trailer>
  <rating>8.6</rating>
  <episodes />
  <episodesnames />
  <writers />
  <gueststars />
  <id>tt0087800</id>
  <releasedate>11.09.1984</releasedate>
  <actor>
    <name>Robert Englund</name>
    <name>Heather Langenkamp</name>
    <name>Johnny Depp</name>
    <name>Ronee Blakley</name>
    <name>John Saxon</name>
    <name>Amanda Wyss</name>
    <name>Jsu Garcia</name>
    <name>Charles Fleischer</name>
    <name>Joseph Whipp</name>
    <name>Lin Shaye</name>
    <name>Joe Unger</name>
    <name>Mimi Craven</name>
    <name>David Andrews</name>
  </actor>
  <genre>
    <name>Horror</name>
    <name>Comedy</name>
  </genre>
  <director>
    <name>Wes Craven</name>
  </director>
  <runtime>91</runtime>
  <certification>R</certification>
  <studio>
    <name>New Line Cinema</name>
  </studio>
  <country>
    <name>United States of America</name>
  </country>
  ...
  ...
  ...
</movie>

Thanks to Henk Holterman, I was able to clean up the code that is processing the XML, and I'm now using the following:

var doc = XDocument.Load(n);  // takes care of all Open/Close issues
strTitle = doc.Root.Element("title") == null ? "" : doc.Root.Element("title").Value;
strYear = doc.Root.Element("year") == null ? "" : doc.Root.Element("year").Value;
strPlot = doc.Root.Element("plot") == null ? "" : doc.Root.Element("plot").Value;
strRating = doc.Root.Element("rating") == null ? "" : doc.Root.Element("rating").Value;
strMPAA = doc.Root.Element("mpaa") == null ? "" : doc.Root.Element("mpaa").Value;
strCertification = doc.Root.Element("certification") == null ? "" : doc.Root.Element("certification").Value;

Now for the last bit, how can I get the Genres from the genre Element using this method? I can't search for the name Element since it is used in various other Elements. I wasn't sure if I could work with:

doc.Root.Element("genre").ElementsAfterSelf("name");

Wasn't clear on what that returns (I don't QUITE understand how to use IEnumberables), or how it would handle multiple "names". I figure it can be done with LINQ, but I am still trying to figure out how to use that.

Thank you very much!!

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

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

发布评论

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

评论(1

掩于岁月 2024-11-26 09:33:43

您可以获得流派列表:

// untested
List<string> genres = doc.Root
      .Element("genre")
      .Elements("name")
      .Select(x => x.Value).ToList(); 

You can get a list of genres:

// untested
List<string> genres = doc.Root
      .Element("genre")
      .Elements("name")
      .Select(x => x.Value).ToList(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文