再遇XML读取问题

其实挺郁闷的,写了这么多关于XML操作的代码还是不熟悉使用C#对XML进行操作...昨晚在一个网站上面听歌,发现可以浏览它的歌曲列表XML文件,想把歌都下载下来,顺手写了个程序...同时熟悉了一下委托的用法(其实自己对委托非常不熟悉..)

最后的迅雷调用方式没有写好,,有空补上..昨晚生成那些LST列表不能实现重命名资源搞到我很郁闷的说..

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.IO;
using System.Net;

namespace DjmaxSongLoader
{
    
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //<embed src='http://pub.maniax.kr/djmax/ost/MP3Player.swf?configURL=http://pub.maniax.kr/djmax/ost/all.xml'
    
//type='application/x-shockwave-flash'
    
//width='480' height='636'
    
//bgcolor='#333333'
    
//allowFullScreen='false'> 
    
//</embed>
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////

    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();

            tbXMLUrl.Text 
= "http://pub.maniax.kr/djmax/ost/all.xml";
            tbMusicUrl.Text 
= "http://pub.maniax.kr/djmax/ost/mp3/";
        }

        
/// <summary>
        
/// 获取内容线程
        
/// </summary>
        private Thread thread;

        
/// <summary>
        
/// 对主界面的操作,使用委托
        
/// </summary>
        
/// <param name="str"></param>
        private delegate void WriteTextBox(StringBuilder str);

        
/// <summary>
        
/// 线程开启
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            
this.Text += " 运行ing...";
            ThreadStart threadStart 
= new ThreadStart(this.GetXMLInfo);
            thread 
= new Thread(threadStart);
            thread.Start();
        }

        
/// <summary>
        
/// 委托控制界面
        
/// </summary>
        
/// <param name="str"></param>
        private void SetTextBox(StringBuilder str)
        {
            tbSongInfo.Text 
= str.ToString();

            
this.Text = this.Text.Split(' ')[0];
        }

        
/// <summary>
        
/// 分析XML
        
/// </summary>
        private void GetXMLInfo()
        {
            StringBuilder str 
= new StringBuilder();

            
string url = tbXMLUrl.Text;

            XmlDocument xmlDoc 
= new XmlDocument();
            
//xmlDoc.Load(url);
            xmlDoc.LoadXml(GetWebInfo(url).Replace("&""&amp;"));

            XmlNodeList nodeList 
= xmlDoc.SelectSingleNode("config").ChildNodes;
            
foreach (XmlNode node in nodeList)
            {
                XmlElement xe 
= (XmlElement)node;

                
if (xe.Name == "song")
                {
                    XmlNodeList xnl 
= xe.ChildNodes;

                    
string fileName = "";
                    
string title = "";
                    
string artist = "";

                    
foreach (XmlNode xn1 in xnl)
                    {
                        XmlElement xe2 
= (XmlElement)xn1;

                        
if (xe2.Name == "artist")
                        {
                            artist 
= xe2.InnerText;
                        }
                        
if (xe2.Name == "title")
                        {
                            title 
= xe2.InnerText;
                        }
                        
if (xe2.Name == "fileName")
                        {
                            
if (xe2.InnerText != "null.mp3")
                                fileName 
= xe2.InnerText;
                            
else
                                fileName 
= "";
                        }

                        
if (fileName != "")
                        {
                            str.AppendFormat(
"{4}{0}?/{1}[{2}].{3}\r\n", fileName, title, artist, fileName.Split('.')[1], tbMusicUrl.Text);
                        }
                    }
                }
            }

            WriteTextBox wtb 
= new WriteTextBox(SetTextBox);
            
this.BeginInvoke(wtb, str);

            
try
            {
                
this.thread.Abort();
            }
            
catch { }
        }

        
/// <summary>
        
/// 获取网页内容
        
/// </summary>
        
/// <param name="url"></param>
        
/// <returns></returns>
        private string GetWebInfo(string url)
        {
            
try
            {
                
string rl;
                WebRequest Request 
= WebRequest.Create(url);
                WebResponse Response 
= Request.GetResponse();
                Stream resStream 
= Response.GetResponseStream();
                StreamReader sr 
= new StreamReader(resStream, Encoding.Default);
                StringBuilder sb 
= new StringBuilder();
                
while ((rl = sr.ReadLine()) != null)
                {
                    sb.Append(rl);
                }

                
return sb.ToString();
            }
            
catch (Exception ex)
            {
                
return ex.ToString();
            }
        }

        
/// <summary>
        
/// 调用迅雷下载
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void btXunleiDown_Click(object sender, EventArgs e)
        {
            
//vbs
            
//Set ThunderAgent = CreateObject("ThunderAgent.Agent.1") Call ThunderAgent.AddTask("http://www.hxit.net/files/bjhyn.mp3","北京欢迎你.mp3","c:\a\","","http://www.readlog.cn",1,0,5) Call ThunderAgent.CommitTasks2(1) Call ThunderAgent.AddTask("http://file.fzone.cn/upload2/hompyFile/2007/28/921524670987.wma","放羊的星星.wma","c:\a\b\c\","","http://www.readlog.cn",1,0,5) Call ThunderAgent.CommitTasks2(1) Set ThunderAgent = Nothing
        }
    }
}
原文地址:https://www.cnblogs.com/Zoya/p/1620084.html