随笔-348  评论-598  文章-0  trackbacks-0
    /// <summary>
    
/// XML解析器
    
/// </summary>

    public class XmlParsers
    
{
        
/// <summary>
        
/// 公共解析方法
        
/// </summary>
        
/// <param name="nodeList"></param>
        
/// <param name="treeNode"></param>
        
/// <param name="table"></param>

        public static void Parse(XmlNodeList nodeList, TreeNodeCollection treeNode, Hashtable table)
        
{
            
foreach (XmlNode node in nodeList)
            
{
                
if (node.NodeType != XmlNodeType.Element)
                
{
                    
continue;
                }


                TreeNode newTreeNode 
= new TreeNode();
                newTreeNode.Text 
= node.Attributes["name"].Value;

                
//父节点
                if (node.HasChildNodes)
                
{
                    Parse(node.ChildNodes, newTreeNode.Nodes, table);
                    treeNode.Add(newTreeNode);
                }

                
else
                
{
                    
//子节点
                    try
                    
{
                        
//如果存在有id的节点则加入到table中
                        
//GISNode gNode = new GISNode();
                        
//gNode.Id = node.Attributes["id"].Value;
                        
//gNode.Name = node.Attributes["name"].Value;
                        
//gNode.Area = node.Attributes["area"].Value;
                        
//gNode.Latitude = node.Attributes["latitude"].Value;
                        
//gNode.Longitude = node.Attributes["longitude"].Value;
                        
//table.Add(gNode.Id, gNode);
                        
//treeNode.Add(gNode.Id, gNode.Name);

                        treeNode.Add(node.Attributes[
"id"].Value, node.Attributes["name"].Value);
                    }

                    
catch (Exception ex)
                    
{
                        
//否则丢弃
                    }


                }


            }

        }


        
/// <summary>
        
/// 解析设备树的方法
        
/// </summary>
        
/// <param name="filePath"></param>
        
/// <param name="tv"></param>
        
/// <param name="table"></param>

        public static void ParseEquipmentXml(string filePath, TreeView tv, Hashtable table)
        
{
           
            XmlDocument xmlDoc 
= new XmlDocument();
            xmlDoc.Load(@filePath);

            XmlNodeList nodeList 
= xmlDoc.DocumentElement.ChildNodes;

            tv.BeginUpdate();
            tv.Nodes.Clear();
            Parse(nodeList, tv.Nodes, table);
            tv.EndUpdate();
        }

    }
简单实用,不过如果XML都不相同的话可能要写多个函数,后面有时间重构一下,通用一点。

---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2008-02-05 02:47 TiGERTiAN 阅读(931) 评论(0)  编辑  收藏 所属分类: DotNetMapXtreme

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问