Project requirements
Using the C # language, the XML file that holds the program information for the saved channel and channel under the disk is displayed through the TreeView control and the DataGridView control and provides the ability to play.
Practice Knowledge points
- Dynamically loading the TreeView node
- Simple operations for parsing XML files in C #
- Simple Read and write operation of Io stream
Architecture Building
We know: "Before you do a new project, you need to build the structure of the project, that is, to design the project in detail."
Through the analysis of requirements "saved channels and channels corresponding to the program information" We can know we need to create two classes-Channel class (channel) and program class (Programs) and their specific properties we can refer to the XML document design.
We can obviously abstract the properties of a class from an XML document
- Channeltype (channel type)
- Tvchannel (channel name)
- Path (channel corresponding to the XML document that holds the program information)
There can be more than one program message per channel
So there's a set of saved program objects under the channel object.
Properties of the Program object
What do we do when we see that two channels of XML documents are stored differently?
We can abstract a full range of attributes with two XML documents
- PlayTime (broadcast time)
- Meridien (broadcast time period)
- ProgramName (program name)
- Path (program's storage paths)
Abstraction of a method
Save the Path property of the channel object by saving the program information XML document paths we can define a method for parsing an XML document in a channel class. However, two channels of the corresponding XML document to save the program information is not the same way, so we can define the channel in the abstract method. and define a subclass of two channel to override its method.
and the program object needs to have a playback method to play it
In our design, we can get the following class architecture. Of course, follow-up can also increase the class according to demand. The class diagram is as follows:
About the factory class (Channeltool)
The function of the factory class is to create the corresponding channel object according to the needs of the program running.
The key code is as follows:
Public StaticList<channels>Load () {List<channels> channels =NewList<channels>(); XmlDocument Channe=NewXmlDocument (); Channe. Load ("Fils\\fullchannels.xml"); XmlNode Root=Channe. DocumentElement; foreach(XmlNode IteminchRoot. ChildNodes) {Channels CL=NULL; Switch(item["Channeltype"]. InnerText) { Case "TypeA": Cl=NewTypeachannel (); Break; Case "TypeB": Cl=NewTypebchannel (); Break; } CL. ChannelName= item["Tvchannel"]. InnerText; All Type= item["Channeltype"]. InnerText; All Path= item["Path"]. InnerText; Channels. ADD (CL); } returnchannels; }
Dynamically binding the TreeView
Binding the TreeView is parsing the XML document that holds the channel information in a collection, and then traversing the entire collection to convert each item in the collection to the node object of the TreeView control and then add the node object to the TreeView control by using the Nodes.Add () method.
Displaying program information using DataGridView
First, the Tag property of the selected TreeNode object is converted to the channel type variable (the channel object is saved in the Tag property of the TreeNode node, the Text property holds the channel name), The load () method that invokes the parse program information XML for the channel object assigns a value to the programs binding variable for the channel object (the collection of program objects is saved in programs). Use the DataSource property of DataGridView to bind the programs collection of channel objects to display in DataGridView.
Key code for the Load method of the channel object parsing XML document
Public Override voidLoad () {XmlDocument TypeA=NewXmlDocument (); Typea.load (Path); XmlNode Root=typea.documentelement; XmlNode tvtable= root["tvprogramtable"]; foreach(XmlNode Iteminchtvtable.childnodes) {Tvprogram program=NewTvprogram (); Program. PlayTime= item["PlayTime"]. InnerText; Program. Meridien= item["Meridien"]. InnerText; Program. ProgramName= item["ProgramName"]. InnerText; Program. Path= item["Path"]. InnerText; This. Programs.add (program. Playtime,program); } }
The above code is to parse the corresponding XML document of Beijing TV station, Phoenix TV's Load method is similar to this, here no longer attaches the code!
Analysis of network TV elves ' thinking