C#解析.XSPF(XML)播放列表
本文关键字:播放列表 XML 解析 XSPF | 更新日期: 2023-09-27 18:29:48
我需要获取"title"(文件夹)属性的值以及播放列表中每个标题中的所有"tid"(id)编号这是.xspf
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/" version="1">
<title>Playlist</title>
<trackList>
<track>
<location>file:///E:/Downloads/video1.mp4</location>
<duration>681493</duration>
<extension application="http://www.videolan.org/vlc/playlist/0">
<vlc:id>0</vlc:id>
</extension>
</track>
<track>
<location>file:///E:/Downloads/video2.mp4</location>
<duration>614585</duration>
<extension application="http://www.videolan.org/vlc/playlist/0">
<vlc:id>1</vlc:id>
</extension>
</track>
<track>
<location>file:///E:/Downloads/video3.mp4</location>
<duration>220629</duration>
<extension application="http://www.videolan.org/vlc/playlist/0">
<vlc:id>2</vlc:id>
</extension>
</track>
</trackList>
<extension application="http://www.videolan.org/vlc/playlist/0" xmlns="http://xspf.org/ns/0/">
<vlc:node title="oooh11111.xspf" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/">
<vlc:node title="BillyJ">
<vlc:item tid="0" />
<vlc:item tid="1" />
<vlc:item tid="2" />
<vlc:item tid="3" />
<vlc:item tid="4" />
<vlc:item tid="5" />
<vlc:item tid="6" />
<vlc:item tid="7" />
<vlc:item tid="8" />
<vlc:item tid="9" />
<vlc:item tid="10" />
<vlc:item tid="57" />
<vlc:item tid="58" />
<vlc:item tid="59" />
</vlc:node>
<vlc:node title="Solo">
<vlc:item tid="60" />
<vlc:item tid="61" />
<vlc:item tid="62" />
<vlc:item tid="63" />
<vlc:item tid="64" />
<vlc:item tid="65" />
<vlc:item tid="66" />
<vlc:item tid="67" />
<vlc:item tid="102" />
<vlc:item tid="103" />
<vlc:item tid="104" />
</vlc:node>
<vlc:node title="Best">
<vlc:item tid="105" />
<vlc:item tid="106" />
<vlc:item tid="107" />
<vlc:item tid="108" />
<vlc:item tid="109" />
<vlc:item tid="110" />
<vlc:item tid="111" />
</vlc:node>
</extension>
</playlist>
但是现在我需要得到"title"(文件夹)属性的值以及每个标题中的所有"tid"(id)编号:
BillyJ
0
1
2
3
4
5
。。。
独奏
60
61
62
63
64
所以我可以通过他们的id 将文件排序到文件夹
以下是我如何解析位置持续时间和id:
class PlaylistItem
{
public string location { get; set; }
public string extensions { get; set; }
public string duration { get; set; }
}
private void button2_Click(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load(@"1.xspf");
XNamespace ns = ((XElement)xdoc.FirstNode).Name.Namespace;
XNamespace nx = ("http://www.videolan.org/vlc/playlist/ns/0/");
List<PlaylistItem> fileSignatures = (from file in xdoc.Descendants(ns+ "track")
select new PlaylistItem
{
location = (string)file.Element(ns + "location").Value,
extensions = (string)file.Element(ns + "extension").Value,
duration = (string)file.Element(ns + "duration")
}).ToList();
foreach (var item in fileSignatures)
{
if (item.duration != null)
{
//Remove %20 (spaces)
var file = System.Net.WebUtility.UrlDecode(item.location);
//trim file:/// from start
char[] trimChar = { 'f', 'i', 'l', 'e','/',':' };
file = file.TrimStart(trimChar);
listBox1.Items.Add("Duration: " + item.duration + " id: " + item.extensions);
listBox1.Items.Add("File: " + file);
}
else
{
}
}
}
试试这个。您有两个带有"node"的标记,只有一个结束标记。
private void button2_Click(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load(FILENAME);
XElement playlist = (XElement)xdoc.FirstNode;
XNamespace ns = playlist.Name.Namespace;
XNamespace nx = ("http://www.videolan.org/vlc/playlist/ns/0/");
XElement extension = playlist.Elements().Where(x => x.Name.LocalName == "extension").FirstOrDefault();
XNamespace nsExtension = extension.Name.Namespace;
XElement node = extension.Elements().Where(x => x.Name.LocalName == "node").FirstOrDefault();
var nodes = node.Elements().Where(x => x.Name.LocalName == "node").Select(y => new {
title = y.Attribute("title").Value,
ids = y.Elements().Where(x => x.Attribute("tid") != null).Select(x => int.Parse(x.Attribute("tid").Value)).ToList()
}