知识点 词汇表 联系我们
按类别浏览
内容搜索    
当前位置:WEB开发技术知识库(www.cn-web.com) .: WEB技术相关 .: XML .: 对Xml文件中结点的搜索匹配

对Xml文件中结点的搜索匹配


用SelectSingleNode()和SelectNodes()搜索结点
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();//建立文档对象
            try
            {
                doc.Load("../../myOrder.xml");
                XmlNode root = doc.DocumentElement;//获取文档的根节点
                XmlNode temp;
                temp = root.SelectSingleNode("姓名");
                Console.WriteLine("(查找1)" + temp);
                temp = root.SelectSingleNode("定购人信息/姓名");
                Console.WriteLine("(查找2)" + temp.Name+":"+temp.InnerText);
                temp = root.SelectSingleNode("订货信息/商品/品名");
                Console.WriteLine("(查找3)" + temp.Name + ":" + temp.InnerText);
                XmlNodeList templist = root.SelectNodes("订货信息/商品/品名");
                Console.WriteLine("(查找4)");
                foreach (XmlNode nodeinlist in templist)
                {
                    Console.WriteLine(nodeinlist.Name + ":" + nodeinlist.InnerText);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();//辅助代码,用于保留控制台窗口
        }
    }
}



在xml搜索节点(两种方法)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();//建立文档对象
            try
            {
                doc.Load("../../myOrder.xml");
                //在xmlDocument对象中搜索元素
                Console.WriteLine("");
                XmlNodeList myNodeList = doc.GetElementsByTagName("品名");
                for (int i = 0; i < myNodeList;i++ )
                {
                    Console.WriteLine(myNodeList[i].Name+":"+myNodeList[i].InnerText);
                }
                //在xmlElement对象中搜索元素
                Console.WriteLine("在xmlElement对象中搜索元素");
                XmlElement myElement = doc.DocumentElement;
                myElement = (XmlElement)myElement.LastChild;
                myNodeList = myElement.GetElementsByTagName("品名");
                for (int i = 0; i < myNodeList; i++)
                {
                    Console.WriteLine(myNodeList[i].Name + ":" + myNodeList[i].InnerText);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();//辅助代码,用于保留控制台窗口
        }
    }
}


Google
 

对此文章打分评级

用户评论

增加评论
此文章还没有任何评论!
网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号