访问xml数据

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();//建立文档对象
            try
            {
                doc.Load("../../myOrder.xml");//加载xml对象
                XmlNode root = doc.DocumentElement;//获取文档根节点
                XmlNode tempNode;
                if (root.HasChildNodes)
                {
                    tempNode = root.FirstChild;//根结点的第一个字节点
                }
                else
                {
                    tempNode = root;

                }
                while (tempNode != root)
                {
                    Console.Write(tempNode.Name);
                    tempNode = tempNode.FirstChild;//获取节点的字节点
                    //判断是否是文本节点
                    if (tempNode.GetType().Name == "XmlText")
                    {
                        Console.WriteLine(":" + tempNode.Value);
                        while (tempNode.NextSibling == null && tempNode != root)
                        {
                            tempNode = tempNode.ParentNode;
                        }
                        if (tempNode != root)
                            tempNode = tempNode.NextSibling;//获取下一个节点

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

你可能感兴趣的:(xml)