Analysis XML application for primary school students

Source: Internet
Author: User

Analysis XML application for primary school students

1. What is XML?

Parsing: XML: Extensible Markup Language (Extensible Markup Language)

HTML: HyperLink Text Markup Language (Hypertext Markup Language)

2. What is the difference between an xml file and an html file?

Resolution: 01. xml is case sensitive and html is not.

02. xml is not a compilation language. xml and html are both interpreted languages.

03.html is used to display data, while Xml files are used to store data.

NOTE: If we encounter a problem when writing an xml file, if we use encoding = "UTF-8", try to cut it into gb2312

3. xml writing notes

1. xml files are case sensitive.
2. Tag pairing
3. the xml document can have only one root node
4. custom labels in xml documents

4. parse xml files

Resolution: 01. Create an xml document

02. copy to the debug directory

03. Create an XmlDocument object Ctrl +. Import the namespace

04. Call doc. Load ("path ")

05. Get the root node XmlNode root = doc. DocumentElement;

06. Use foreach to get subnode content

5. Problem records

01. Load (string filename): absolute path: "D: \ 123 \ Singer. xml"

02. item. Name indicates <content>, and item. InnerText indicates <> content </>

6. Exercise

 

Requirement Description click the TV station Channel node, and the DataGridView display channel corresponding program list

 

 

The main code is as follows:

1 // TV program type 2 public class TvProgram 3 {4 // broadcast time 5 public DateTime PlayTime {get; set;} 6 7 // time period 8 public string Median {get; set;} 9 // program name 10 public string ProgramName {get; set;} 11 // program file path 12 public string FilePath {get; set;} 13}
1 // channel class 2 public abstract class ChannelBase3 {4 public string channelName; // channel name 5 public string path; // channel strength 6 public List <TvProgram> programList; // program list 7 // parse channel program information 8 public abstract void Fetch (); 9}
1 // TypeA channel class 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 // channel B class 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;
// Parse the XMl file and fill in the Data 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 // factory operation class 2 public class ChannelManager 3 {4 public Dictionary <string, ChannelBase> dic = new Dictionary <string, ChannelBase> (); // store the channel name and program information in the channel. 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); // create channel A or channel B object 13. channelName = item ["tvChannel"]. innerText; // obtain the channel name 14 channel in XML. path = item ["path"]. innerText; // obtain the program path strength of the corresponding channel stored in XML. add (channel. channelName, channel); 16} 17} 18 // create a class instance 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 // create a ChannelManagement Class Object 2 ChannelManager manager = new ChannelManager (); 3 // load data in the TreeView 4 private void LoadTreeView () 5 {6 TreeNode nodeFristLevel = new TreeNode ("My TV station"); 7 this. tvChannel. nodes. add (nodeFristLevel); 8 TreeNode allnode = new TreeNode ("all TV stations"); 9 TreeNode node = null; 10 manager. loadtvChannel (); 11 // cyclically Add the subnode 12 foreach (ChannelBase item in manager) to "all TV stations. dic. values) 13 {14 node = New TreeNode (item. channelName); 15 node. tag = item; // Save the ChannelBse object 16 allnode. nodes. add (node); 17} 18 this. tvChannel. nodes. add (allnode); 19} 20 private void Form1_Load (object sender, EventArgs e) 21 {22 // skin setting 23 skinEngine1.SkinFile = "MSN. ssk "; 24 // call Method 25 LoadTreeView (); 26 27} 28 // after clicking TreeView, event 29 private void tvChannel_AfterSelect (object sender, TreeViewEventArgs e) 30 {31 if (this. tvChannel. selec TedNode. Level = 1) // judge the depth of the channel to be 132 {33 ChannelBase channel = (ChannelBase) tvChannel. SelectedNode. Tag; 34 if (channel. programList! = Null) // clear the data 35 {36 channel in the List <T> set. programList. clear (); 37} 38 channel. fetch (); 39 this. dgvProgList. dataSource = channel. programList; // bind the data source 40} 41 42}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.