对于Windows Phone中的Webradio应用程序,我正在尝试使用数据读取XML文件,但我遇到了特定字段的问题。 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>
使用下一个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"),
};
但是,我找不到一种方法来获取音频流。有'audiostreams'元素,它有3个孩子'audiostream'元素,我需要'streamurl'。
最好是存储3个streamurls,以便我可以在以后改变质量。然后我应该在Class Station中有一个字段:
String[] streamurls = {www.1.mp3, www.2.mp3, www.3.mp3};
并将3个streamurls存储在那里以便稍后选择。我已经尝试了一些与XML,属性和XElement相关的内容,但是我无法让它工作。
那里有人知道某种方式吗?
顺便说一下,我真的不知道如何在这里突出显示代码和内容,我希望它有效,其他我真的很抱歉......
答案 0 :(得分:2)
如果您真的只想要这些网址,您可以执行以下操作(假设IEnumerable<string>
上名为string[]
的{{1}}(或StreamUrls
}类型的属性:
Station
如果您想从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()
}
元素中获取其他信息,请声明类似的类:
audiostream
然后查询如下:
class Stream
{
public string Url { get; set; }
public string Codec { get; set; }
public string Rate { get; set; }
public bool IsDefault { get; set; }
}
(如果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()
}
和Codec
只能包含某些值,则将其表示为Rate
将优于enum
。)
答案 1 :(得分:0)
希望它有用
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();
}
}
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>