Silverlight 中 读取XML文件

一:xml文件

二:后台代码

private void ReadXml()
        {
            List<TreeViewItem> nodes = new List<TreeViewItem>();
            XmlReader xmlReader = XmlReader.Create("Config.xml");
            StringBuilder sbXml = new StringBuilder();
            while (xmlReader.Read())
            {
                sbXml.Append("\n");
                sbXml.Append("节点类型:" + xmlReader.NodeType.ToString());
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.DocumentType:
                        break;
                    case XmlNodeType.Element:
                        //得到节点的名称
                        string elementName = xmlReader.Name;//循环节点的属性
                        for (int i = 0; i < xmlReader.AttributeCount; i++)
                        {
                            xmlReader.MoveToAttribute(i);if (xmlReader.Value.Trim() != "" && xmlReader.Name.Trim() == "Title")
                            {
                                TreeViewItem node = new TreeViewItem();
                                node.Header = xmlReader.Value;
                                nodes.Add(node);
                            }
                        }
                        break;
                    case XmlNodeType.EndElement:
                        elementName = xmlReader.Name;
                        break;
                    default:
                        break;
                }
            }
            treeView1.ItemsSource = nodes;
        }

三:运行结果

posted @ 2015-02-09 17:28  HongMaJu  阅读(495)  评论(0编辑  收藏  举报