Initial knowledge of XML and simple factory application--Network TV Genie

Source: Internet
Author: User

Network TV Genie Task Description

1. Parsing the data in an XML file

Three simple XML files;

<?xml version="1.0" encoding="utf-8" ? ><tvchannele >  <Channel>    <channelType>TypeA</channelType>    <tvChannel> Beijing TV </ tvchannel>    <path>files/Beijing TV .xml</path>  </Channel>  <Channel>    <channelType>TypeB</channelType>    <tvChannel> Phoenix TV </tvChannel>    <path> files/Phoenix satellite TV .xml</path>  </Channel></TVChannele>
Fullchannels
<?xml version="1.0"encoding="Utf-8"? ><typea Version ="1.0"> <channelName> Beijing TV </channelName><!--TV--<tvProgramTable> <tvProgram> <p Laytime> --9- in  .: Geneva</playTime><!--Program broadcast time-<meridien> morning stalls </meridien><!--time--<programName> Replay: Country    Inter-line </programName><!--program name-<path>E:\</path>< Local path to!--program video--</tvProgram> <tvProgram> <playTime> --9- in  -:xx</playTime> <!--broadcast time-<meridien> morning </meridien> <!---<program Name> Beijing You </programName> <!--program name--<path>E:\</path> <!--Local path to the program video---& Lt;/tvprogram> <tvProgram> <playTime> --9- in  the: Geneva</playTime> <!--broadcast time-<meridien> morning </meridien> <!---<program Name> replay: "Red Star Theatre" 32 episodes: Yong Invincible </programName> <!--program name--<path>E:\</path> <!--program Video Ground path-</tvProgram> </tvProgramTable></typeA>
Beijing TV. Xml
<?xml version="1.0"encoding="Utf-8"? ><typeb Version ="1.0"> <channelName> Phoenix TV </channelName> <!--TV--<tvProgramTable> <tvProgram> &lt ;p laytime> --9- in  on: -</playTime> <!--broadcast time-<meridien> morning </meridien> <!---<program    Name> Replay: International Double Line </programName> <!--program name--<path>E:\</path> <!--Local path to the program video-- </tvProgram> </tvProgramTable></typeB>
Phoenix satellite TV. Xml

2. Add all the channels in the "Fullchannels.xml" file to the form's TreeView control.

3. Select a channel in the TreeView control to display the program list for that channel in the form's Datagrideview control.

construction of the project framework

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

1. Create the corresponding class

ChannelFactory (Simple Factory): Create a channel subclass from a simple factory

 Public classChannelFactory {//Create a channel subclass from a factory        Public StaticChannelbase CreateChannel (stringtype) {Channelbase Channel=NULL; Switch(type) { Case "TypeA": Channel=NewTypeachannel ();  Break;  Case "TypeB": Channel=NewTypebchannel ();  Break; default:                    Break; }           returnChannel; }    }
ChannelFactory

ChannelManager (Channel management Class): Parse the "fullchannels.xml" file, create a collection that stores all channel information, and add the channel name to the TreeView control

Core Ideas

1. Create a DAO management class, add a method to load all channel information (add after Code) Loadallchannel ()
When we pick a node, we actually get the channel object corresponding to the text value of the node. So use dctionary<> to store all the channel objects

        PublicChannelManager () {fullchannels=Newdictionary<string, channelbase>(); }       //define a container that hosts all channel collections (Key: Channel name Value: Channel object)       Privatedictionary<string, channelbase>Fullchannels;  Publicdictionary<string, channelbase>Fullchannels {Get{returnFullchannels;} Set{fullchannels =value;} }
Fullchannels

2. The next step is to add data to the TreeView control in the form, parsing: disassembling the collection data in the dictionary by using foreach, each of which corresponds to a TreeNode object, what we need is to "all stations" The name attribute of the station is added under the node. and the name attribute corresponds to a channel object, which we store with the Tag property.

       //01. Write a method to parse the XML and load the data into the box        Public voidparsexmltodictionary () {XmlDocument doc=NewXmlDocument (); Doc. Load ("Files/fullchannels.xml"); //Get root nodeXmlNode root=Doc.           DocumentElement; foreach(XmlNode IteminchRoot. ChildNodes) {//an item represents a channel node//Get Type               stringtype=item["Channeltype"].               InnerText; Channelbase Channel=Channelfactory.createchannel (type); //nameChannel. ChannelName = item["Tvchannel"].               InnerText; Channel. Path= item["Path"].               InnerText; Channel. Type=type; Fullchannels.add (channel.           Channelname,channel); }       }
parsexmltodictionary
        Private voidFrmmain_load (Objectsender, EventArgs e) {           //carry the data from the container fullchannels in the Channelmanage class to the TreeView,//According to the idea, the value of each item in the collection is a channel correspondence, not traversing to a//channel object, we form a TreeNode on TV.//01. How to load the root nodeLoadrootnode (); //02. Split the data in the collection into "multiple sub-nodes under all stations"ChannelManager manager=NewChannelManager (); Manager.            Parsexmltodictionary (); Dictionary<string,channelbase> dic=Manager.            Fullchannels; foreach(varChannelinchDiC. Values) {//conversion ProcessTreeNode tn=NewTreeNode (); Tn. Text=Channel.                ChannelName; Tn. Tag=Channel; //let TN add to all stationsAllNode.Nodes.Add (TN); }        }
Binding of Node objects

3. Display the program list of the currently selected station in DGV (Datagrideview), simply set the DataSource property of the DGV to the program list of the currently selected station list<progeram>.

(We should know one thing when we select a node, when we click on the node has got to the station's program list, get the information should happen when we click on the node or should happen after the selection)

        Private voidTvchannel_afterselect (Objectsender, TreeViewEventArgs e) {                      //01. Get to the node selected by the userTreeNode SelectedNode =Tvchannel.selectednode; if(selectednode.level==1&&selectednode.parent.text=="All Stations") {channelbase channel=(channelbase) Selectednode.tag; //Clear Collection                if(channel. tvlist!=NULL) {channel.                Tvlist.clear (); } channel.                Fetch (); List<TvProgram> prolist =Channel.                Tvlist; Dgvproglist.datasource=prolist; }                  }
Tvchannel_afterselect

Write here, if you run the program, will report an error

.

We recall that when we add a node to the TreeView control, we bind a specific channel object to the node's Tag property. But the TreeView before the form is displayed, the first node is selected by default

So we want to optimize it, that is, when the form is open, the default selected node is judged by the line

  if (selectednode.level==1&&selectednode.parent.text==" all stations ")
default selection of node judgment

We will find that when we click on the same station many times, the TV program information will be added in DGV, so the following processing

// Clear Collection                if (channel. tvlist!=null)                {                    channel. Tvlist.clear ();                }
Cleanup Program List

4. The node right-click menu is added, the different nodes in the TreeView control correspond to different right-click menus, when we click on "My TV" byte point, the right-click menu can only be deleted, and when I click on "All Stations" under the Sub-node, the right-click menu for adding to "My TV" and "Delete, If we use multiple ContextMenuStrip to bind different nodes by line, we can create redundancy, could we use a contextmenustrip to bind to multiple nodes and show them different menus? The answer is yes, We need to use a property of ContextMenuStrip: Visible.

We want to determine the selected node after selecting the node, and choose to display the node's right-click menu item

            if(TvAllChannel.SelectedNode.Level = =1&& TvAllChannel.SelectedNode.Parent.Text = ="All 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 Stations") {ctxaddtomychannels.items[0]. Visible =false; ctxaddtomychannels.items[1]. Visible =false; ctxaddtomychannels.items[3]. Visible =false;
node Right-click menu item

Here, this small project can be said to be over, then learned that IO Stream will continue to optimize the project. Maybe what I do is not perfect, there are many code reuse and redundancy, and I hope to see you come up with some points to help me get more better solutions. The boy thanked me here.

Initial knowledge of XML and simple factory application--Network TV Genie

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.