使用linq获取特定值到xml

时间:2012-12-20 23:14:31

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

我正在使用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);

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

1 个答案:

答案 0 :(得分:3)

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

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