2

我正在开发 Windows 8 Metro 应用程序,我需要从 XML 文件中获取一些信息。我用 LINQ to XML 解析它,但我遇到了问题。这是 XML:

<feed xmlns="http://www.allocine.net/v6/ns/">
  <page>1</page>
  <count>1</count>
  <results type="movie">10</results>
  <results type="person">0</results>
  <totalResults>10</totalResults>
  <movie code="61282">
    <originalTitle>Avatar</originalTitle>
    <title>Avatar</title>
    <productionYear>2009</productionYear>
    <release>
      <releaseDate>2010-09-01</releaseDate>
    </release>
    <castingShort>
      <directors>James Cameron</directors>
      <actors>Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang, Michelle Rodriguez</actors>
    </castingShort>
    <statistics>
      <pressRating>4.33333</pressRating>
      <userRating>4.31338</userRating>
    </statistics>
    <poster path="/medias/nmedia/18/78/95/70/19485155.jpg" 
            href="http://images.allocine.fr/medias/nmedia/18/78/95/70/19485155.jpg"/>
    <linkList>
      <link rel="aco:web" 
            href="http://www.allocine.fr/film/fichefilm_gen_cfilm=61282.html"/>
    </linkList>
  </movie>
</feed>

我需要获取“电影”节点的“代码”值和节点“链接”的“href”值,但我尝试的所有事情都失败了......

您可以认为 XML 的开头是<movie>因为我获取了文件并对其进行了解析以保持 XML 的整洁。我的文件以<movie code="">

对于像“演员”这样的经典价值,我这样做:

Actors = (string)query.Element("castingShort").Element("actors")

它运行良好!我的问题是带有名称的特定值。

编辑 :

这就是我对你的建议所做的。

        var group1 = new SampleDataGroup("Group-1", "Films", "", "Assets/icone_groupe_all_movies.jpg", "Films sur le DD");
        movieName = "avatar";
        Uri = "http://api.allocine.fr/rest/v3/search?partner=YW5kcm9pZC12M3M&filter=movie,person&count=1&page=1&q=" + movieName + "&format=xml";
        var httpResponse = await new HttpClient().GetAsync(Uri);
        string sourceCode = get_new_xml(await httpResponse.Content.ReadAsStringAsync());
        XDocument doc = XDocument.Parse(sourceCode);
        XNamespace ns = "http://www.allocine.net/v6/ns/";
        XElement movie = doc.Root.Element(ns + "movie");
        XElement castingShort = movie.Element(ns + "castingShort");
        XElement statistics = movie.Element(ns + "statistics");
        Data data = new Data
        {
            MovieCode = (string)movie.Attribute("code"),
            OriginalTitle = (string)movie.Element(ns + "originalTitle"),
            Title = (string)movie.Element(ns + "title"),
            ProductionYear = (string)movie.Element(ns + "productionYear"),
            Directors = (string)castingShort.Element(ns + "directors"),
            Actors = (string)castingShort.Element(ns + "actors"),
            PressRating = (string)statistics.Element(ns + "pressRating"),
            UserRating = (string)statistics.Element(ns + "userRating"),
            Cover = (string)movie.Element(ns + "linkList").Element(ns + "link").Attribute("href")
        };
        group1.Items.Add(new SampleDataItem("Group-1-Item-1", data.Title, data.Cover, data.ProductionYear, "", data.ReleaseDate, group1));
        this.AllGroups.Add(group1);

但不幸的是它仍然没有工作......

4

1 回答 1

3

因此,您在 xml 中声明了命名空间,您应该声明并初始化一个 XNamespace 对象,并在指定 XName 对象(元素方法的参数)时使用它:

XDocument xdoc = XDocument.Load(path_to_xml);
XNamespace ns =  "http://www.allocine.net/v6/ns/";
XElement movie = xdoc.Root.Element(ns + "movie");
var code = (int)movie.Attribute("code");
var href = (string)movie.Element(ns + "linkList")
                        .Element(ns + "link").Attribute("href");

如果您只有一个<movie>元素,那么您不需要对电影元素序列进行操作并将单个电影视为列表。只需获取movie节点并通过解析该节点创建新的数据对象:

XNamespace ns =  "http://www.allocine.net/v6/ns/";
XElement movie = xdoc.Root.Element(ns + "movie");
XElement castingShort = movie.Element(ns + "castingShort");
XElement statistics = movie.Element(ns + "statistics");
Data data = new Data
{
    MovieCode = (int)movie.Attribute("code"),
    OriginalTitle = (string)movie.Element(ns + "originalTitle"),
    Title = (string)movie.Element(ns + "title"),
    ProductionYear = (string)movie.Element(ns + "productionYear"),
    Directors = (string)castingShort.Element(ns + "directors"),
    Actors = (string)castingShort.Element(ns + "actors"),
    PressRating = (string)statistics.Element(ns + "pressRating"),
    UserRating = (string)statistics.Element(ns + "userRating"),
    Cover = (string)movie.Element(ns + "linkList")
                         .Element(ns + "link").Attribute("href") 
};
于 2012-12-20T23:24:55.323 回答