1.什么是XML?
解析:XML:ExtensibleMarkupLanguage(可擴展標記語言)
HTML:HyperLinkTextMarkupLanguage(超文本標記語言)
2. xml文件和html文件的區別?
解析:01.xml嚴格區分大小寫,html不區分
02.xml不是編譯語言,xml和html都是解釋型語言
03.html語言負責顯示數據,而Xml文件就是專門用來存儲數據
注:如果我們在書寫xml文件的時候,如果自己用的encoding="utf-8"出現問題,那么試著切成gb2312
3.xml書寫注意點
1.xml文件嚴格區分大小寫2.標簽配對出現3.xml文檔只能有一個根節點4.我們自定義xml文檔中的標簽
4.解析xml文件
解析:01.做一個xml文檔
02.copy到debug目錄下
03.創建一個XmlDocument對象Ctrl+.導入命名空間
04.調用doc.Load(“路徑”)
05.拿到根節點XmlNoderoot=doc.DocumentElement;
06.用foreach來獲取子節點內容
5.問題記錄
01.Load(stringfilename):絕對路徑:"D:/123/Singer.xml"
02.item.Name指定的是<內容>,item.InnerText指的是<>內容</>
6.練習
需求說明 單擊電視臺頻道節點,DataGridView顯示頻道對應節目單

主要代碼如下:
1 //電視節目類 2 public class TvPRogram 3 { 4 //播出時間 5 public DateTime PlayTime { get; set; } 6 7 //時段 8 public string Median { get; set; } 9 //節目名稱10 public string ProgramName { get; set; }11 //節目文件路勁12 public string FilePath { get; set; }13 }
1 //頻道類2 public abstract class ChannelBase3 {4 public string channelName;//頻道名稱5 public string path;//頻道路勁6 public List<TvProgram> programList;//節目列表7 //解析頻道節目信息8 public abstract void Fetch();9 } 1 //TypeA類頻道類 2 public class TypeAChannel:ChannelBase 3 { 4 public TypeAChannel() { } 5 6 7 public override void Fetch() 8 { 9 XmlDocument doc = new XmlDocument();10 doc.Load(path);11 if (programList==null)12 {13 programList=new List<TvProgram>();14 }15 XmlNode root = doc.DocumentElement;16 foreach (XmlNode item in root.ChildNodes)17 {18 if (item.Name=="tvProgramTable")19 {20 foreach (XmlNode child in item.ChildNodes)21 {22 TvProgram program = new TvProgram();23 program.PlayTime = Convert.ToDateTime(child["playTime"].InnerText);24 program.Median = child["meridien"].InnerText;25 program.ProgramName = child["programName"].InnerText;26 program.FilePath = child["path"].InnerText;27 this.programList.Add(program);28 }29 }30 }31 }32 } 1 //頻道B類 2 public class TypeBChannel:ChannelBase 3 { 4 5 public override void Fetch() 6 { 7 XmlDocument doc = new XmlDocument(); 8 doc.Load(path); 9 if (programList == null)10 {11 programList = new List<TvProgram>();12 }13 XmlNode root = doc.DocumentElement; //解析XMl文件并填充數據14 foreach (XmlNode item in root.ChildNodes)15 {16 if (item.Name == "ProgramList")17 {18 foreach (XmlNode child in item.ChildNodes)19 {20 TvProgram program = new TvProgram();21 program.PlayTime = Convert.ToDateTime(child["playTime"].InnerText);22 program.FilePath = child["path"].InnerText;23 program.ProgramName = child["name"].InnerText;24 this.programList.Add(program);25 }26 }27 }28 }29 } 1 //工廠操作類 2 public class ChannelManager 3 { 4 public Dictionary<string, ChannelBase> dic = new Dictionary<string, ChannelBase>();//存儲頻道名稱和該頻道中的節目信息 5 public void LoadtvChannel() 6 { 7 XmlDocument doc = new XmlDocument(); 8 doc.Load("files/FullChannels.xml"); 9 XmlNode node = doc.DocumentElement;10 foreach (XmlNode item in node.ChildNodes)11 {12 ChannelBase channel = CreateChannel(item["channelType"].InnerText);//創建頻道A或者B對象13 channel.channelName = item["tvChannel"].InnerText;//獲取XML中頻道名稱14 channel.path = item["path"].InnerText;//獲取XML中存儲的相應頻道節目路勁15 dic.Add(channel.channelName,channel);16 }17 }18 //創建類的實例19 public ChannelBase CreateChannel(string type)20 {21 ChannelBase channel = null;22 switch (type)23 {24 case"TypeA":25 channel = new TypeAChannel();26 break;27 case"TypeB":28 channel = new TypeBChannel();29 break;30 default:31 break;32 }33 return channel;34 }35 } 1 //創建ChannelManagement類對象 2 ChannelManager manager = new ChannelManager(); 3 //加載TreeView中的數據 4 private void LoadTreeView() 5 { 6 TreeNode nodeFristLevel = new TreeNode("我的電視臺"); 7 this.tvChannel.Nodes.Add(nodeFristLevel); 8 TreeNode allnode = new TreeNode("所有電視臺"); 9 TreeNode node = null;10 manager.LoadtvChannel();11 //循環給“所有電視臺”添加子節點12 foreach (ChannelBase item in manager.dic.Values)13 {14 node = new TreeNode(item.channelName);15 node.Tag = item;//保存ChannelBse對象16 allnode.Nodes.Add(node); 17 }18 this.tvChannel.Nodes.Add(allnode);19 }20 private void Form1_Load(object sender, EventArgs e)21 {22 //皮膚設置23 skinEngine1.SkinFile = "MSN.ssk";24 //調用方法25 LoadTreeView();26 27 }28 //TreeView點擊后事件29 private void tvChannel_AfterSelect(object sender, TreeViewEventArgs e)30 {31 if (this.tvChannel.SelectedNode.Level==1)//判斷深度為132 {33 ChannelBase channel = (ChannelBase)tvChannel.SelectedNode.Tag;34 if (channel.programList != null)//清空List<T>集合中的數據35 {36 channel.programList.Clear();37 }38 channel.Fetch();39 this.dgvProgList.DataSource = channel.programList;//綁定數據源40 }41 42 }新聞熱點
疑難解答