C#解析.XSPF(XML)播放列表
C# parsing .XSPF (XML) Playlist
我需要获取 "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()
}
我需要获取 "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()
}