C# 使用linq到xml获取特定值

C# 使用linq到xml获取特定值,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我正在开发Windows8 metro应用程序,需要从XML文件中获取一些信息。 我用LINQ将其解析为XML,但我遇到了一个问题。 以下是XML: <feed xmlns="http://www.allocine.net/v6/ns/"> <page>1</page> <count>1</count> <results type="movie">10</results> <results

我正在开发Windows8 metro应用程序,需要从XML文件中获取一些信息。 我用LINQ将其解析为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>
它工作得很好!我的问题是带有名称的特定值

编辑:

我就是这么接受你的建议的

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

但不幸的是,它仍然不起作用

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


如果只有一个
元素,则不需要对电影元素序列进行操作,也不需要将单个电影视为列表。只需获取
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。。。你能解释一下吗?因为当我这么做的时候working@bottus刚刚验证-它为我提供了电影代码
61282
。可能您已经更改了某些内容-设置一个断点并验证movie元素的值。@bottus请参阅。另外,我引入了
movie
变量,只是为了使查询更简单(因此两个查询都需要获得movie元素)。这就是我所做的:当我将“MovieCode=(int)movie.Attribute(“code”),”设置为注释时,我的应用程序正在工作,但当我让代码中的行没有注释时,一开始我告诉过你,我的xml是从节点电影开始的,而不是提要,但是现在没有解析,这给了我“新的xml”,它工作得很好!!!非常感谢您抽出时间来lazyberezovsky!!
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");
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") 
};