½«XMLÊý¾ÝÌî³äµ½TreeViewÖÐ
Îĵµ¼°´úÂëÏÂÔØ£ºhttp://www.dingos.cn/index.php?topic=1899.0
½éÉÜ
µ±²Ëµ¥ÏîÊǾ³£¸Ä±äʱ£¬´ÓXMLÎĵµÖд´½¨TreeView²Åµ±ÊǷdz£ÓÐÓõġ£ÀýÈ磬ʹÓÃXML×÷ΪÊý¾Ý¿â´æ´¢¼Ç¼¡£
ÕâÀïÓиö¼òµ¥Ê¾Àý¡£ÒÔ·À»ìÏýÕâ¸öÀý×ӱȽϼòµ¥¡£
±³¾°
¶ÔXML¡¢TreeView¿Ø¼þµÄÓ¦Óã¬ÔÚVisual StudioÖÐÓÐÖúÓÚÀí½âÕâЩ²½Öè¡£
ʹÓôúÂë
×¢Ò⣺Õâ¸ö½â¾ö·½°¸£¨xml2treeviewmenuSolution£©ÊÇVS2010´´½¨¡£
Ìí¼ÓÒ»¸öXMLÎļþµ½ÏîÄ¿Öв¢ÃüÃûΪ“menu.xml”¡£¸ù¾Ý²Ëµ¥Ïî±à¼XMLÎļþ¡£
<?xml version="1.0" encoding="utf-8" ?>
<root>
<folder title='folder 1a' >
<record title='record 1a1' />
<record title='record 1a2' />
<folder title='folder 1b'>
<record title='record 1b1' />
</folder>
</folder>
<folder title='folder 2a' >
<record title='record 2a1' />
</folder>
<folder title='folder 3a' >
<record title='record 3a1' />
<record title='record 3a2' />
</folder>
</root>
´ÓVisual
Studio¹¤¾ßÏäÖн«TreeView¿Ø¼þÍÏÈëWindows´°ÌåÖС£ÔÚÕâ¸öÀý×Ó£¬ÃüÃû¿Ø¼þΪ“treeViewMenu”¡£Ê¹ÓÃusingÌí¼Ó¶ÔXMLÀàµÄÒýÓá£
using System.Xml;
using System.Xml.XPath;
´´½¨Ò»¸öXMLDocument¶ÔÏóÓÃÓÚ±£´æÎļþ¡£
public partial class Form1 : Form{
private XmlDocument docXML = new XmlDocument();
µ±´°Ìå¼ÓÔØʱ£¬´ÓXMLÎļþÖмÓÔØÌî³äTreeView¿Ø¼þµÄXMLÎĵµ¡£
private void Form1_Load(object sender, EventArgs e){
docXML.Load("menu.xml"); // ¼ÓÔØXMLÎļþ
populateBaseNodes(); // Ìî³äËùÓиù½Úµã
}
´ÓµÚÒ»¼¶<folder>½Úµã¿ªÊ¼Ìî³ä¡£½«Ã¿¸ö¸ù½ÚµãÌí¼Óµ½Ê÷ÖУ¬²¢Îªµ±Ç°¸ù¾ÝµãÌí¼Ó×ӽڵ㡣
private void populateBaseNodes(){
treeViewMenu.Nodes.Clear(); // Çå³þÒÑ´æÔÚµÄÏî
treeViewMenu.BeginUpdate(); // ¿ªÊ¼¸üÐÂTreeView
TreeNode treenode;
treenode = treeViewMenu.Nodes.Add("Folders");
XmlNodeList baseNodeList = docXML.SelectNodes("root/folder");
// »ñµÃµÚÒ»²ã <folder> ½Úµã
foreach (XmlNode xmlnode in baseNodeList){
// ±äÁ¿ËùÓеĸù½Úµã <
Ïà¹ØÎĵµ£º
²úÆ·¼¸ÄêǰʹÓÃASP£¬ºóÀ´Éý¼¶µ½.Net 1.1£¬ÔÙÉý¼¶µ½2.0£¬Ò»Ö±¶¼ÓÐÓÃXSLTת»»XMLÉú³ÉÍøÒ³µÄ·½Ê½£¬ÉÔ΢ÕûÀíÏ¡£
XML file:
<?xml version="1.0" encoding="utf-8" ?>
<ric>
<catalog>
<book price ......
1. ¾ßÓÐÁ¼ºÃµÄ¸ñʽ
XML Îĵµ¸ñʽÊôÓÚÁ¼ºÃ¸ñʽµÄÎļþ¡£HTML ÎļþÖеıê¼Ç£¬ÓÐЩÊDz»ÐèÒª½áβ±ê¼ÇµÄ£¬Èç<br>£¬ÓÐЩÍøҳȱÉÙÈô¸É½áβ±ê¼Ç£¬ÕÕÑùÄÜÕýÈ·ÏÔʾ¡£¶øXML µÄ±ê¼ÇÒ»¶¨ÒªÓµÓнáβ±ê¼Ç£¬ÀýÈ磺
<name>ÕÅÈý</name>
¼´£¬XML ±ê¼ÇÒ»¶¨ÊdzÉË«³É¶ÔµÄ¡£Èç¹ûûÓнáβ±ê¼Ç£¬ÄÇôÔÚ½áÊøµÄ“>”Ç ......
<mce:script language="javascript" type="text/javascript"><!--
//ÐèÒª¶ÁÈ¡µÄxmlÎļþ
var uRl = "jsReadXml.xml";
var xmlDoc;
//³õʼ»¯£¬¸øÉÏÊö¶¨Òå±äÁ¿¸³Öµ
// function showcurcity(){
if(window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM&q ......
books.xml:
Xml´úÂë
<?xml version="1.0" encoding="UTF-8"?>
<books>
<!--This is a test for dom4j, jakoes, 2007.7.19-->
<book show="yes" ......