First knowledge of XML and simple factory application-network TV Genie and xml network TV genie

Source: Internet
Author: User

First knowledge of XML and simple factory application-network TV Genie and xml network TV genie
Network TV genieTask Description

1. parse the data in the XML file

Three simple xml files;

<? Xml version = "1.0" encoding = "UTF-8"?> <TVChannele> <Channel> <channelType> TypeA </channelType> <tvChannel> Beijing TV station </tvChannel> <path> files/Beijing TV station. xml </path> </Channel> <channelType> TypeB </channelType> <tvChannel> Phoenix TV </tvChannel> <path> files/Phoenix TV. xml </path> </Channel> </TVChannele>FullChannels <? Xml version = "1.0" encoding = "UTF-8"?> <TypeA version = "1.0"> <channelName> Beijing TV </channelName> <! -- TV station --> <tvProgramTable> <tvProgram> <playTime> </playTime> <! -- Program broadcast time --> <meridien> early morning </meridien> <! -- Time Period --> <programName> REPLAY: International dual-line </programName> <! -- Program name --> <path> E :\</path> <! -- Local path of the program video --> </tvProgram> <playTime> </playTime> <! -- Program broadcast time --> <meridien> early morning </meridien> <! -- Time Period --> <programName> Beijing! </programName> <! -- Program name --> <path> E :\</path> <! -- Local path of the program video --> </tvProgram> <playTime> </playTime> <! -- Program broadcast time --> <meridien> early morning </meridien> <! -- Time Period --> <programName> REPLAY: 32 episodes of red star theater: invincible brave </programName> <! -- Program name --> <path> E :\</path> <! -- Local path of the program video --> </tvProgram> </tvProgramTable> </typeA>Beijing TV. xml <? Xml version = "1.0" encoding = "UTF-8"?> <TypeB version = "1.0"> <channelName> Phoenix TV </channelName> <! -- TV station --> <tvProgramTable> <tvProgram> <playTime> </playTime> <! -- Program broadcast time --> <meridien> early morning </meridien> <! -- Time Period --> <programName> REPLAY: International dual-line </programName> <! -- Program name --> <path> E :\</path> <! -- Local path of the program video --> </tvProgram> </tvProgramTable> </typeB>Phoenix TV. xml

2. Add all channels in the "FullChannels. xml" file to the TreeView control of the form.

3. Select a channel from the TreeView control and display the program list of the channel in the DataGrideView control of the form.

Construction of the project framework

Analyze the approximate structure of the network Genie and create corresponding classes

1. Create the corresponding class

ChannelFactory: Creates a channel subclass through a simple factory.

Public class ChannelFactory {// create channel subclass public static ChannelBase CreateChannel (string type) {ChannelBase channel = null; switch (type) {case "TypeA ": channel = new TypeAChannel (); break; case "TypeB": channel = new TypeBChannel (); break; default: break;} return channel ;}}ChannelFactory

 

ChannelManager (channel management class): parses the "FullChannels. xml" file, creates a collection of all channel information, and adds the channel name to the TreeView control.

Core Ideas

1. Created a channel management class and added a method for loading information of all channels (added after code) LoadAllChannel ()
When a node is selected, the channel object corresponding to the Text value of the node is actually obtained. Therefore, Dctionary <> is used to store all channel objects.

Public ChannelManager () {fullChannels = new Dictionary <string, ChannelBase> () ;}// defines a container that carries all the channel sets (Key: channel name Value: Channel object) private Dictionary <string, ChannelBase> fullChannels; public Dictionary <string, ChannelBase> FullChannels {get {return fullChannels;} set {fullChannels = value ;}}FullChannels

2. the next step is to add data to the TreeView control in the form, and analyze the collection data in the dictionary through foreach. Each item corresponds to a TreeNode object, we need to add the TV station name attribute to the "all TV stations" node. the name attribute corresponds to a Channel object, which is stored using the Tag attribute.

// 01. write a method to parse the xml and install the public void ParseXmlToDictionary () {XmlDocument doc = new XmlDocument (); doc. load ("files/Fullchannels. xml "); // get the root node XmlNode root = doc. documentElement; foreach (XmlNode item in root. childNodes) {// an item represents a Channel node // string type = item ["channelType"]. innerText; ChannelBase channel = ChannelFactory. createChannel (type); // name channel. channelName = item ["tvChannel"]. innerText; channel. path = item ["path"]. innerText; channel. type = type; fullChannels. add (channel. channelName, channel );}}ParseXmlToDictionary private void FrmMain_Load (object sender, EventArgs e) {// upload the data in the FullChannels container in the ChannelManage class to the TreeView. // based on your ideas, the Value of each item in the set is a channel correspondence. If no channel object is traversed, A TreeNode // 01 on TV is formed. method for loading the root node LoadRootNode (); // 02. split the data in the set into multiple sub-nodes: ChannelManager manager = new ChannelManager (); manager. parseXmlToDictionary (); Dictionary <string, ChannelBase> dic = manager. fullChannels; foreach (var channel in dic. values) {// conversion process TreeNode tn = new TreeNode (); tn. text = channel. channelName; tn. tag = channel; // Add tn to allNode on all radio stations. nodes. add (tn );}}Binding node objects

3. display the program list of the selected radio station in dgv (DataGrideView). Set the datasource attribute of dgv to the list of programs of the selected TV station <Progeram>.

(When selecting a node, we should be clear about one thing. After clicking the node, we have obtained the program list of the TV station, the obtained information should occur when we click a node or after the node is selected)

Private void tvChannel_AfterSelect (object sender, TreeViewEventArgs e) {// 01. obtain the selected node TreeNode selectedNode = tvChannel. selectedNode; if (selectedNode. level = 1 & selectedNode. parent. text = "all TV stations") {ChannelBase channel = (ChannelBase) selectedNode. tag; // clear the set if (channel. tvList! = Null) {channel. TvList. Clear ();} channel. Fetch (); List <TvProgram> proList = channel. TvList; dgvProgList. DataSource = proList ;}}TvChannel_AfterSelect

Write it here. If you run the program, an error will be reported.

.

When adding nodes to the TreeView control, we bound a specific channel object to the TAG attribute of the node. however, the first node is selected by default before the TreeView is displayed in the form.

 

So we need to optimize it, that is, when the form is opened, we can determine the default Node

If (selectedNode. Level = 1 & selectedNode. Parent. Text = "all TV stations ")Default Node Selection

 

 

We will find that when we click on the same TV station multiple times, the program information of the TV station will be added multiple times in dgv, so we will proceed with the following:

// Clear the if (channel. TvList! = Null) {channel. TvList. Clear ();}Clear program list

4. right-click node menu addition. Different Nodes in the TreeView control correspond to different right-click menus. When we click the byte points under "my TV station", the right-click menu can only be deleted, when I click subnodes under "all TV stations", right-click them and choose "my TV station" and "delete". If we use multiple ContextMenuStrip to bind different nodes, can we bind a ContextMenuStrip to multiple nodes and display different menus? The answer is yes. we need to use a ContextMenuStrip attribute: Visible.

 

After selecting a node, we need to determine the selected node and select the right-click menu item to display the node.

If (tvAllChannel. selectedNode. level = 1 & tvAllChannel. selectedNode. parent. text = "all TV stations") {ctxAddToMyChannels. items [2]. visible = false; ctxAddToMyChannels. items [3]. visible = false;} else if (tvAllChannel. selectedNode. level = 1 & tvAllChannel. selectedNode. parent. text = "my TV station") {ctxAddToMyChannels. items [0]. visible = false; ctxAddToMyChannels. items [1]. visible = false; ctxAddToMyChannels. items [2]. visible = false;} else if (tvAllChannel. selectedNode. text = "all TV stations") {ctxAddToMyChannels. items [0]. visible = false; ctxAddToMyChannels. items [1]. visible = false; ctxAddToMyChannels. items [3]. visible = false;Right-click node menu item

Now, this small project has come to an end, and I/O Stream learning will continue to optimize the project. maybe what I do is not perfect. There are a lot of code reuse and redundancy. I also hope you can give me some suggestions to help me get more and better solutions. thank you ~~~

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.