| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1837 人关注过本帖
标题:如何用C#获取XML中的任意节点值?
只看楼主 加入收藏
dragon_xml
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2009-9-2
结帖率:22.22%
收藏
已结贴  问题点数:10 回复次数:2 
如何用C#获取XML中的任意节点值?
如何用C#获取XML中的任意节点值,比如:
  <aaa name="book" value="0">
    <key name="we" value="111" />
  </aaa>
  <bbb name="dictionary" value="1">
    <key name="fg" value="222" />
  </bbb>
  <ccc name="Magazine" value="2">
    <key name="hj" value="333" />
  </ccc>

不是子节点,是根节点?
用xmlNode.NextSibling.Name只能得到下一个节点值,如果要得到第三个或第四个怎么获取?
搜索更多相关主题的帖子: 获取 节点 XML 
2009-09-21 14:42
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:5 
reader = new XmlTextReader("*.xml");

while (reader.Read())
{
       。。。;
}

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-21 21:42
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:5 
程序代码:
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Xml; 
 
namespace ReadIndex 
{ 
    public class XmlBaseInfoReader 
    { 
        private string xmlPath; 
        private XmlElement root; 
        private XmlDocument doc; 
        private List<NodeWithID> nodeByOrder; 
        private int currentIndex = 1; 
 
        public string XmlPath 
        { 
            get 
            { 
                return this.xmlPath; 
            } 
            set 
            { 
                this.xmlPath = value; 
            } 
        } 
        public List<NodeWithID> NodeByOrder 
        { 
            get 
            { 
                FillNodeList(); 
                return this.nodeByOrder; 
            } 
        } 
 
        public bool IsRoot(XmlElement Ele) 
        { 
            if (this.root == Ele) 
            { 
                return true; 
            } 
            else 
            { 
                return false; 
            } 
        } 
 
        private void FillNodeList() 
        { 
            TraversalNode(this.root); 
        } 
 
        private void TraversalNode(XmlNode Node) 
        { 
            foreach (XmlNode n in Node.ChildNodes) 
            { 
                NodeWithID node = new NodeWithID(this.currentIndex, n); 
                this.nodeByOrder.Add(node); 
                this.currentIndex++; 
                TraversalNode(n); 
            } 
        } 
 
        public XmlBaseInfoReader(string XmlPath) 
        { 
            this.xmlPath = XmlPath; 
            this.doc = new XmlDocument(); 
            this.nodeByOrder = new List<NodeWithID>(); 
            try 
            { 
                doc.Load(this.xmlPath); 
                this.root = doc.DocumentElement; 
            } 
            catch 
            { 
                throw new Exception("Xml文档路径错误"); 
            } 
        } 
    } 
 
    public struct NodeWithID 
    { 
        public int id; 
        public XmlNode node; 
 
        public NodeWithID(int id, XmlNode N) 
        { 
            this.id = id; 
            this.node = N; 
        } 
    } 
 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            XmlBaseInfoReader testXml = new XmlBaseInfoReader("Test.xml"); 
            foreach (NodeWithID i in testXml.NodeByOrder) 
            { 
                Console.WriteLine(i.id); 
                Console.WriteLine(i.node.Attributes["name"].Value); 
            } 
 
            Console.ReadKey(); 
        } 
    } 
} 
不知道对你有没有帮助。
我是通过递归把Xml的节点全部遍历,然后按照顺序编号...要其他的信息可以自己加。
2009-09-21 23:14
快速回复:如何用C#获取XML中的任意节点值?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022946 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved