开发者

LINQ to XML: Query Expression for a tree where some values are attributes and others are node values

开发者 https://www.devze.com 2022-12-27 03:45 出处:网络
I am trying to write a query expression to parse an XML tree, but without much luck. The tree is as follows:

I am trying to write a query expression to parse an XML tree, but without much luck.

The tree is as follows:

<item> 
    <itemInfo id="1965339" lang="en" key="title"> 
      <info>Octopuzzle&#xd;</info> 
    </itemInfo> 
    <itemInfo id="1965337" lang="en" key="longDescription"> 
      <info>&quot;In Octopuzzle you play the Octopus on a mission! Escape the dangerous reef, and save it in the process. To do so you’ll have to make it through 20 challenging levels.&#xd;
The game offers a real brain teasing levels packed with interactive sea creatures and objects that will keep you hooked for more. Along the way you’ll have shooting challenges, cannons to jump from, meet armoured fish, and many more surprises the deep-sea has to offer.&#xd;
Are you ready for the deep-sea puzzle adventure?&#xd;
&quot;&#xd;</info> 
    </itemInfo> 
    <itemInfo id="1965335" lan开发者_如何学Pythong="en" key="shortDescription"> 
      <info>In Octopuzzle you play the Octopus on a mission! Escape the dangerous reef, and save it in the process. To do so you’ll have to make it through 20 challenging levels.&#xd;</info> 
    </itemInfo> 
</item>

I load into into a XElement without any problems.

What I need to do is get the values of title, short description, and long description respectively for a given value of the lang attribute, in this case en.

Can anyone help? Thanks


Sure, something like:

private string GetValue(XElement element, string language, string key)
{
    return element.Elements("itemInfo")
                  .Where(x => (string) x.Attribute("lang") == language)
                  .Where(x => (string) x.Attribute("key") == key)
                  .Select(x => (string) x.Element("info"))
                  .FirstOrDefault();
}
...
string title = GetValue(item, "en", "title");
string longDescription = GetValue(item, "en", "longDescription");
string shortDescription = GetValue(item, "en", "shortDescription");

If you've already got the relevant item element, I don't think you really want a query expression; if you're querying over multiple elements you might. For example:

var query = from item in doc.Descendants("item")
            select new {
                Title = GetValue(item, "en", "title"),
                LongDescription = GetValue(item, "en", "longDescription"),
                ShortDescription = GetValue(item, "en", "shortDescription");
            };

Or in non-query-expression form:

var query = doc.Descendants("item")
               .Select(item => new {
                    Title = GetValue(item, "en", "title"),
                    LongDescription = GetValue(item, "en", "longDescription"),
                    ShortDescription = GetValue(item, "en", "shortDescription");
               };
0

精彩评论

暂无评论...
验证码 换一张
取 消