C # learning notes XML and tree menus

Source: Internet
Author: User

C # learning notes: XML and tree menus

-------------------------------------- XML -----------------------------------
XML file Overview
XML is called an extensible markup language. It is short for extensible markup language. It is a very important part in the. NET Framework. It is used to describe data and is a powerful tool for processing structured document information.
XML is widely used. The most basic configuration information of websites and applications is generally described in XML files. For example, the Web Service uses XML to define the standard format for data transmission between applications.

PS: XML is a markup language. All tags must appear in pairs. tags are case-sensitive (also known as nodes ). Nodes used to describe data can be freely expanded.

Parse XML files
① The xmldocument object can represent the entire XML document. It uses the load () method to read the specified XML file into the xmldocument object. The parameters of the load () method are the path of the XML document. The documentelement attribute is used to obtain the root node of an XML file.
② The xmlnode object represents a node in XML. The childnodes attribute is used to obtain all subnodes under the node. The name attribute of the node can be used to obtain the name of the current node (the name attribute is used to obtain the node name), and The innertext attribute of the node is used to obtain the value of the current node. The object attributes and methods for operating XML are shown in the table below.

Object Attributes and Methods
Description
Xmldocument documentelement
Get Root Node
Childnodes
Retrieve all child nodes
Load ()
Read the entire XML structure
Xmlnode innertext
Current node Value
Name of the current node
Childnodes
All subnodes of the current node
Simple Example
Xmldocument xmldoc = new xmldocument ();
// Load the XML document
Xmldoc. Load ("fullchannels. xml ");
// Obtain the root node
Xmlelement xmlroot = xmldoc. documentelement;
// Traverse channel nodes
Foreach (xmlnode item in xmlroot. childnodes)
PS: xmldoc ["node name"] ["node name"]. innertext to get node content

Case: add an XML record locally in the memo

XML file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Note>
<Day year = "2013" month = "09" Day = "09">
<To> Home </to>
<From> school </from>
<Heading> hide <Message> message </message>
</Day>
</Note>
① Obtain the specified root node and use selectsinglenode () to obtain the name of the input node for a root node
Xmldocument XM = new xmldocument ();
// Load the specified XML file
XM. Load ("node. xml ");
// Obtain the specified Root Node
Xmlelement Xe = (xmlelement) XM. selectsinglenode ("NOTE ");
② Add a specified root node and use createelement () to input the node name
// Add a specified Root Node
Xmlelement xitem = XM. createelement ("day ");
// Add the day node to the root node
Xe. appendchild (xitem );
③ Assign values to the day node attribute. Use setattribute () to input the attribute name and attribute value.
// Add the attribute value for the day root node
Xitem. setattribute ("year", this.txt year. Text. Trim ());
Xitem. setattribute ("month", this.txt month. Text. Trim ());
Xitem. setattribute ("day", this.txt day. Text. Trim ());
PS: Relationship between xmlnode and xmlelement. xmlelement has attributes but xmlnode does not. Xmlelement is a subclass of the xmlnode subclass.
④ Add a subnode to the day node and use appendchild (). Subnode Input
// Add a subnode to the day Node
Xitem. appendchild (XM. createelement (""));
Xitem ["to"]. innertext = this.txt to. Text. Trim ();
Xitem. appendchild (XM. createelement ("from "));
Xitem ["from"]. innertext = this.txt from. Text. Trim ();
Xitem. appendchild (XM. createelement ("heading "));
Xitem ["heading"]. innertext = this.txt heading. Text. Trim ();
Xitem. appendchild (XM. createelement ("message "));
Xitem ["message"]. innertext = this.txt message. Text. Trim ();
⑤ Save the File Save () in the last step and input the file path
// Save the file
XM. Save ("node. xml ");
---------------------------------- I am a split line -------------------------------
// Obtain the information of the last memorandum
Xmlelement Xe = (xmlelement) XM ["NOTE"]. childnodes [XM ["NOTE"]. childnodes. Count-1];
// Obtain node attributes
Xe. getattribute ("year"); // This method is available only for xmlelement.
----------------------------------- Xmlreader --------------------------------
① Create an xmlreader
Xmlreader xr = xmlreader. Create ("node. xml ");
② Use xmlreader to read XML
-- For my personal understanding of xmlreader, xmlreader should read from top to bottom from left to right, not based on nodes. Therefore, we need to determine whether the currently read data type is the node name or the node value. Because the read name or read node value is regarded as a loop, a temporary variable is required to receive the read node name.
// Xname is used to receive the name of the read Node
String xname = "";
While (XR. Read ())
{
// The data type currently read
Switch (XR. nodetype)
{
Case xmlnodetype. element:
// Obtain the name of the secondary Node
Xname = XR. localname;
Break;
Case xmlnodetype. Text:
// When reading the value of a node, it is determined based on the obtained node name.
Switch (xname)
{
// Xmlreader reads the attribute
Case "year ":
This.txt Year. Text = xr. Value;
Break;
Case "month ":
This.txt Month. Text = xr. Value;
Break;
Case "day ":
This.txt day. Text = XR. value;
Break;
Case "":
This.txt to. Text = XR. value;
Break;
Case "from ":
This.txt from. Text = XR. value;
Break;
Case "heading ":
This.txt heading. Text = XR. value;
Break;
Case "message ":
This.txt message. Text = XR. value;
Break;
}
Break;

}
}
------------------------------------ Tree menu --------------------------------
-- Get node depth
This. Treeview. selectnode. Level
The node starts from 0 and has the same depth as the first-level node.

-- Get the node text
This. Treeview. selectnode. Text

-- Hide node Information
This. Treeview. Tag = Object

-- Add a node
// Create a Node object
Treenode rootnode = new treenode ("string ")
// Add a node
This. Treeview. nodes. Add (rootnode );
PS: The add () parameter can be of the string type. The added node content is string and can be received by treenode. The added node content is treenode and can be received by Int.

Important attributes and events of Treeview
Attribute description
All Tree nodes in the nodes Treeview Control
Selectnode: The selected node in the current Treeview control. If not selected, the return value is null.
Event Description
Afterselect occurs after the tree node is selected
Beforeexpand events triggered before expanding a node to obtain the current expanded Node

Treenode attributes
Attribute description
Text displayed on the text node
Index of the index node in the collection
Parent obtains the parent node of the node.
Level node levels 0 and 1 in the tree menu ......
Tag node Value
All next-level subnodes of the nodes Node

-- Delete selected nodes
This. Treeview. selectnode. Remove ()

-- Clear the child nodes of the selected node
This. Treeview. selectnode. Clear ()

-- Clear all child nodes of the Treeview Control
This. Treeview. nodes. Clear ()

-- User Experience
Tree menus are often used at the top of the tree Menu update method to ensure that nodes are not repeatedly loaded.
Controls that load information generally have a preprocessing process that clears control elements first, such as listview, which is often used to load information.

-- Hide an option in the shortcut menu
Menu. items [0]. Visible = false; // set the first item of the right-click menu to invisible.

-------------------------------------- Summary ----------------------------------

XML is called an extensible markup language. It is mainly used to describe data.

Read an XML document and use the Document Object. xml nodes use the xmlnode object

The documentelement attribute of the xmldocument object can get the root of the XML document, and the childnodes attribute can get all the child nodes.

Treeview is used to display information with hierarchies. Its main attributes include nodes and selectednode. The nodes attribute contains the Treeview top-level subnode set; selectednode indicates the currently selected node.

Treenode indicates the Node object of the Treeview. The text attribute is used to set the text description of a node. The tag attribute can be used to set information related to the node.

You can use the add () method of treenode to add nodes to the Treeview. The remove () method can remove the specified node. The clear () method can remove all nodes under the specified node.

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.