【问题标题】:getting a specific value using linq to xml使用 linq to xml 获取特定值
【发布时间】:2012-12-20 23:14:31
【问题描述】:

我正在开发 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>

我需要获取“movie”节点的“code”值和节点“link”的“href”值,但我尝试的所有事情都失败了......

您可以认为 XML 的开头是 &lt;movie&gt;,因为我得到了文件并解析它以保持 XML 的整洁。我的文件以&lt;movie code=""&gt;开头

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

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);

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

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:

    因此您在 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");
    

    如果您只有一个&lt;movie&gt; 元素,那么您不需要对电影元素序列进行操作并将单个电影视为列表。只需获取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") 
    };
    

    【讨论】:

    • 非常感谢您的回答,因为 href 它运行良好,但不适用于代码。我不明白为什么你做了一个名为 ns 的新变量和另一个名为 movie 的新变量……你能解释一下吗,因为当我这样做时,它不起作用
    • @bottus 刚刚验证 - 它给了我 61282 的电影代码。也许你已经改变了一些东西 - 设置一个断点并验证你拥有的电影元素的值。
    • @bottus 见working with namespaces in xml。我还引入了movie 变量,只是为了让查询更简单(因此两个查询都需要获取电影元素)。
    • 这就是我所做的:nsa31.casimages.com/img/2012/12/21/121221010432466530.png 当我设置“MovieCode = (int)movie.Attribute("code")”作为评论时,我的应用程序正在运行,但是当我让线路进入没有注释的代码,不是
    • 好吧,一开始我告诉过你,我的 xml 从节点电影开始,而不是提要,但现在没有解析,它给了我“新的 xml”,它工作得很好!非常感谢您抽出宝贵的时间lazyberezovsky !!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多