在 Linq 中以组织不太好的 xml 进行分组

发布于 2024-12-21 02:52:55 字数 873 浏览 1 评论 0原文

我使用 YQL 来获取一些数据,这是我的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<div id="content>
<div id="html">
<h3>City</h3>
<div id="movie">
    <h4>
        <a href="">movie 1</a>
    </h4>
<div>
<div id="movie>
    <h4>
        <a href="">movie 2</a>
    </h4>
</div>
    .
    .
    .

<h3>City 2</h3>
<div id="movie">
    <h4>
        <a href="">movie 1</a>
    </h4>
<div>
<div id="movie>
    <h4>
        <a href="">movie 2</a>
    </h4>
</div>

我想在我的 Windows Phone 应用程序中填充一个列表框,其中包含城市名称及其电影,如下所示:

City 1
Movie1
Movie 2

City 2
Movie1
Movie 2

但是,我被困在这里,因为所有 XML 都在带有 id 内容的 div 内。

如何使用 LINQ 语句来解决这个问题?

Im using YQL to get some data and here is my xml:

<?xml version="1.0" encoding="UTF-8"?>
<div id="content>
<div id="html">
<h3>City</h3>
<div id="movie">
    <h4>
        <a href="">movie 1</a>
    </h4>
<div>
<div id="movie>
    <h4>
        <a href="">movie 2</a>
    </h4>
</div>
    .
    .
    .

<h3>City 2</h3>
<div id="movie">
    <h4>
        <a href="">movie 1</a>
    </h4>
<div>
<div id="movie>
    <h4>
        <a href="">movie 2</a>
    </h4>
</div>

I want to populate a listbox in my windows phone app with the city name and its movies like this:

City 1
Movie1
Movie 2

City 2
Movie1
Movie 2

However, I'm getting stuck here since all the XML is inside the div with id content.

How could a make a LINQ statement to solve this?

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

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

发布评论

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

评论(1

素手挽清风 2024-12-28 02:52:55

我试图将您发布的输入转换为一些格式良好的 XML:

<div id="content">
  <div id="html">
    <h3>City</h3>
    <div id="movie">
      <h4>
        <a href="">movie 1</a>
      </h4>
    </div>
    <div id="movie">
      <h4>
        <a href="">movie 2</a>
      </h4>
    </div>
    .
    .
    .

    <h3>City 2</h3>
    <div id="movie">
      <h4>
        <a href="">movie 1</a>
      </h4>
    </div>
    <div id="movie">
      <h4>
        <a href="">movie 2</a>
      </h4>
    </div>
  </div>
</div>

然后您可以获得一个带有代码的列表

    XDocument doc = XDocument.Load("input.xml");
    List<string> data =
        doc.Descendants("h3")
        .Union(
          doc.Descendants("div")
          .Where(d => (string)d.Attribute("id") == "movie")
          .Elements("h4")
          .Elements("a")
          ).InDocumentOrder()
        .Select(e => e.Value)
        .ToList();

[编辑] 您最初的请求似乎要求一个平面列表结果,您的评论表明您宁愿想要一个分组结构,所以这里是改编样本:

    XDocument doc = XDocument.Load("input.xml");
    var groupedData =
        (from movie in doc.Root.Descendants("div")
         where (string)movie.Attribute("id") == "movie"
         group movie by movie.ElementsBeforeSelf("h3").Last() into g
         select new
         {
             city = g.Key.Value,
             movies = (from m in g
                       select (string)m.Element("h4").Element("a")).ToList()
         }).ToList();

    // now use above list for data binding or 
    // in the simplest case just consume it with foreach:
    foreach (var group in groupedData)
    {
        Console.WriteLine("city: {0}:", group.city);
        foreach (var movie in group.movies)
        {
            Console.WriteLine(movie);
        }
    }

I tried to morph your posted input into some well-formed XML:

<div id="content">
  <div id="html">
    <h3>City</h3>
    <div id="movie">
      <h4>
        <a href="">movie 1</a>
      </h4>
    </div>
    <div id="movie">
      <h4>
        <a href="">movie 2</a>
      </h4>
    </div>
    .
    .
    .

    <h3>City 2</h3>
    <div id="movie">
      <h4>
        <a href="">movie 1</a>
      </h4>
    </div>
    <div id="movie">
      <h4>
        <a href="">movie 2</a>
      </h4>
    </div>
  </div>
</div>

Then you can get a List with the code

    XDocument doc = XDocument.Load("input.xml");
    List<string> data =
        doc.Descendants("h3")
        .Union(
          doc.Descendants("div")
          .Where(d => (string)d.Attribute("id") == "movie")
          .Elements("h4")
          .Elements("a")
          ).InDocumentOrder()
        .Select(e => e.Value)
        .ToList();

[edit] Your initial request seemed to ask for a flat list result, your comment suggests you rather want a grouped structure so here is an adapted sample:

    XDocument doc = XDocument.Load("input.xml");
    var groupedData =
        (from movie in doc.Root.Descendants("div")
         where (string)movie.Attribute("id") == "movie"
         group movie by movie.ElementsBeforeSelf("h3").Last() into g
         select new
         {
             city = g.Key.Value,
             movies = (from m in g
                       select (string)m.Element("h4").Element("a")).ToList()
         }).ToList();

    // now use above list for data binding or 
    // in the simplest case just consume it with foreach:
    foreach (var group in groupedData)
    {
        Console.WriteLine("city: {0}:", group.city);
        foreach (var movie in group.movies)
        {
            Console.WriteLine(movie);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文