C# 解析 XML (.xspf 文件)

C# Parsing XML (.xspf file)

我有一个播放列表需要解析,我需要做一些事情,比如将它们设置在列表框中等。 这是 .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">
        <vlc:node title="oh.xspf">
        <vlc:node title="B">
            <vlc:item tid="0"/>
            <vlc:item tid="1"/>         
        </vlc:node>
        <vlc:node title="Solo">
            <vlc:item tid="2"/>         
        </vlc:node>
    </extension>
</playlist>

这是有效的 C# 代码,但我遇到了问题,因为我使用 "node.ChildNode.Item(1).InnerText" 有些文件的元数据不仅仅是 "location" "duration"。 但我似乎无法使用元素名称来获取我想要的信息

 private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("1.xspf");
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("track");
            string proID = "", proName = "";
            foreach (XmlNode node in nodeList)
            {
                    proID = node.ChildNodes.Item(1).InnerText;//ChildNodes.Item(1).InnerText;
                    //proName = node.SelectSingleNode("duration").InnerText;
                    listBox1.Items.Add(proID);


            }       
        }

使用 xml linq,如下所示。使用 System.Xml.Linq 添加;到模块顶部

        const string FILENAME = @"c:\temp\test.xml";
        XDocument doc = XDocument.Load(FILENAME);
        XNamespace ns = ((XElement)doc.FirstNode).Name.Namespace;
        var data = doc.Descendants(ns + "track").Select(x => new object[] {
            (string)x.Element(ns + "location"),
            (int)x.Element(ns + "duration"),
            int.Parse(x.Element(ns + "extension").Value),
            (string)x.Element(ns + "extension").Attribute("application")
        }).ToList();

        foreach (object prodID in data)
        {
            listBox1.Items.Add(prodID);
        }  

这个有效:

 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;

            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)
                {
                    listBox1.Items.Add("Duration: " + item.duration + "  id: " + item.extensions);
                    listBox1.Items.Add("File: " + item.location);
                }
                else
                {
                    
                }
            }


        }