---
title: "c#遍历XML"
author: "jayzotion"
date: "2009-02-10T09:57:03Z"
tags: []
slug: "c-xml"
draft: false
lastmod: "2009-02-10T10:01:47Z"
id: 325995
url: "https://jayzotion.iteye.com/blog/325995"
---

```c#
public void  GetDataFromXml()
		{
			ArrayList headersubList = new ArrayList();
			//获取当前XML文档的根 一级
			XmlNode oNode = m_oDom.DocumentElement;

			//获取根节点的所有子节点列表 
			XmlNodeList oList = oNode.ChildNodes;

			//标记当前节点
			XmlNode oCurrentNode;
			//遍历所有二级节点
			for (int i = 0; i < oList.Count; i++)
			{
				//二级
				oCurrentNode = oList[i];

				//检测当前节点的名称，节点的值是否与已知匹配
				if (oCurrentNode.Name.ToLower().Equals("store"))
				{
					//检测是否有子节点 三级
					if (oCurrentNode.HasChildNodes)
					{
						Console.WriteLine(oCurrentNode.ChildNodes.Count);

						//遍历当前节点的所有子节点
						for (int n = 0; n < oCurrentNode.ChildNodes.Count; n++)
						{
							//检测当前节点的子节点名称是否与已知匹配
							if (oCurrentNode.ChildNodes[n].Name.ToLower() == "sale")
							{
								//获取该节点所有属性，以供查询
								XmlNamedNodeMap oMap = oCurrentNode.ChildNodes[n].Attributes;

								Hashtable headItem = new Hashtable();
								headItem.Add("Id", oCurrentNode.Attributes.GetNamedItem("Id").Value);
								headItem.Add("Name", oCurrentNode.Attributes.GetNamedItem("Name").Value);
								headItem.Add("OrderNo", oMap.GetNamedItem("OrderNo").Value);
								headItem.Add("Qty", oMap.GetNamedItem("Qty").Value);

								headersubList.Add(headItem);

								Console.WriteLine(oCurrentNode.Attributes.GetNamedItem("Id").Value);
								Console.WriteLine(oCurrentNode.Attributes.GetNamedItem("Name").Value);
								Console.WriteLine(oMap.GetNamedItem("OrderNo").Value);
								Console.WriteLine(oMap.GetNamedItem("Qty").Value);

								Console.ReadLine();
							}
						}
					}
				}
			}
		}


```