简体   繁体   English

在Xelement中选择Xelement中的值?

[英]Select value in Xelement within Xelement?

For a Webradio app in windows Phone, i'm trying to read an XML file with the data, but i'm having a problem with a specific field. 对于Windows Phone中的Webradio应用程序,我正在尝试使用数据读取XML文件,但我遇到了特定字段的问题。 The XML File looks like this: XML文件如下所示:

<brands xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <brandgroup>
        <brand>
            <code>blabla</code>
            <name>blabla</name>
            <logo>blabla</logo>
            <websiteurl>blabla</websiteurl>
            <audiostreams>
                <audiostream streamurl="www.1.mp3" codec="mp3" streamrate="low"/>
                <audiostream streamurl="www.2.mp3" codec="mp3" streamrate="med" default="true"/>
                <audiostream streamurl="www.3.mp3" codec="mp3" streamrate="high"/>
            </audiostreams>
        </brand>
        <brand>
        </brand>
    </brandgroup>
    other 'brandgroups' with other 'brand'
</brand>

With the next Code i'm capable of getting the Name, Code, and Website into and object of Class Station for every brand inside every brandgroup. 通过下一个Code,我能够为每个品牌组中的每个品牌获取Class Station的名称,代码和网站以及对象。


XDocument loadedData = XDocument.Load("netten.xml");
var data = from query in loadedData.Descendants("brand") 
           select new Station
           {
               Name = (string)query.Element("name"),
               Code = (int)query.Element("code"),
               Website = (string)query.Element("websiteurl"), 
           };

However, I can't find a way to get the audiostream. 但是,我无法找到获得音频流的方法。 There is the 'audiostreams' element wich has 3 child 'audiostream' elements, where i need the 'streamurl'. 有'audiostreams'元素,它有3个孩子'audiostream'元素,我需要'streamurl'。

Best would be to store the 3 streamurls so i could change the quality later on. 最好的方法是存储3个streamurls,以便我可以在以后改变质量。 Then i should need to have a field in Class Station: 然后我应该在Class Station中有一个字段:

String[] streamurls = {www.1.mp3, www.2.mp3, www.3.mp3};

and store the 3 streamurls in there to select later on. 并将3个streamurls存储在那里以便稍后选择。 I've tried some things that are posted here related to XML, Attribute and XElement, but i can't get it to work. 我已经尝试了一些与XML,属性和XElement相关的内容,但是我无法让它工作。

Is there anyone out there that knows a way? 那里有谁知道一种方式吗?

Btw I don't really get how to highlight code and stuff here, I hope it works, otherwse I'm really sorry... 顺便说一下,我真的不知道如何突出显示代码和东西,我希望它有效,其他我真的很抱歉......

If you really want just the URLs, you can do something like (assuming the is a property of type IEnumerable<string> (or string[] ) called StreamUrls on Station ): 如果你真的只想要URL,你可以做类似的事情(假设是一个名为StreamUrls on Station IEnumerable<string> (或string[] )类型的属性):

from brand in loadedData.Descendants("brand") 
select new Station
{
    Name = (string)brand.Element("name"),
    Code = (int)brand.Element("code"),
    Website = (string)brand.Element("websiteurl"), 
    StreamUrls = brand.Element("audiostreams")
              .Elements("audiostream")
              .Attributes("streamurl")
              .Select(a => a.Value)
              .ToArray()
}

If you want to get other information from the audiostream elements, declare a class like: 如果要从audiostream元素中获取其他信息,请声明类如下的类:

class Stream
{
    public string Url { get; set; }
    public string Codec { get; set; }
    public string Rate { get; set; }
    public bool IsDefault { get; set; }
}

And the query would then look like: 然后查询将如下所示:

from brand in loadedData.Descendants("brand") 
select new Station
{
    Name = (string)brand.Element("name"),
    Code = (int)brand.Element("code"),
    Website = (string)brand.Element("websiteurl"), 
    Streams =
        (from stream in brand.Element("audiostreams").Elements("audiostream")
        select new Stream
        {
            Url = (string)stream.Attribute("streamurl"),
            Codec = (string)stream.Attribute("codec"),
            Rate = (string)stream.Attribute("streamrate"),
            IsDefault = (string)stream.Attribute("default") == "true"
        }).ToArray()
}

(If Codec and Rate can only have certain values, expressing them as an enum would be better than string .) (如果CodecRate只能有某些值,将它们表示为enum将比string更好。)

Hope it is useful 希望它有用

    static void Main(string[] args)
    {
        XDocument loadedData = XDocument.Load("netten.xml");
        var data = from query in loadedData.Descendants("brand")
                   group query by new { A = query.Element("name"), B = query.Element("code"), C = query.Element("websiteurl"), D = query.Element("audiostreams") } into g
                   select new
                   {
                       Name = g.Key.A + "",
                       Code = g.Key.B + "",
                       Website = g.Key.C + "",

                       AudioStreams = g.Key.D.Elements("audiostream")
                                                .Attributes("streamurl")
                                                .Select(x => x.Value)
                                                .ToArray()

                   };


        foreach (var x in data)
        {
            Console.WriteLine(x.Name);
            Console.WriteLine(x.Code);
            Console.WriteLine(x.Website);

            foreach (var url in x.AudioStreams)
                Console.WriteLine(url);
        }


        Console.ReadKey();
    }
}

The xml file: xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<brands xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <brandgroup>
    <brand>
      <code>blabla</code>
      <name>blabla</name>
      <logo>blabla</logo>
      <websiteurl>blabla</websiteurl>
      <audiostreams>
        <audiostream streamurl="www.1.mp3" codec="mp3" streamrate="low"/>
        <audiostream streamurl="www.2.mp3" codec="mp3" streamrate="med" default="true"/>
        <audiostream streamurl="www.3.mp3" codec="mp3" streamrate="high"/>
      </audiostreams>
    </brand>
  </brandgroup>
  <brandgroup>
    <brand>
      <code>blabla2</code>
      <name>blabla2</name>
      <logo>blabla</logo>
      <websiteurl>blabla2</websiteurl>
      <audiostreams>
        <audiostream streamurl="www.4.mp3" codec="mp3" streamrate="low"/>
        <audiostream streamurl="www.5.mp3" codec="mp3" streamrate="med" default="true"/>
        <audiostream streamurl="www.6.mp3" codec="mp3" streamrate="high"/>
      </audiostreams>
    </brand>
  </brandgroup>
</brands>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM