public ActionResult Index() { XmlReader reader = null; try { //获取路径 ..\TGB.Common.Message\TGB.Common.Message.UI\XML下的XML文件 string xmlPath = System.Web.HttpContext.Current.Server.MapPath("~/XML/SMSContent.xml"); XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true;//忽略文档里面的注释 reader = XmlReader.Create(xmlPath, settings); //加载XML文件 xmlDoc.Load(reader); //取根结点 var root = xmlDoc.DocumentElement; //获取根节点下所有的子节点 XmlNodeList xnlist = root.ChildNodes; //声明短信内容实体集合 List<SMSContentEntity> smslist = new List<SMSContentEntity>(); foreach (XmlNode xn in xnlist) { SMSContentEntity smsentity = new SMSContentEntity(); // 将节点转换为元素,便于得到节点的属性值 XmlElement xe = (XmlElement)xn; smsentity.ProgramKey = xe.Name; // 得到sms节点的所有子节点,并赋值 XmlNodeList xnode = xe.ChildNodes; smsentity.SMSStartcontent = xnode.Item(0).InnerText; smsentity.SMSEndContent = xnode.Item(1).InnerText; smsentity.OpePerson = xnode.Item(2).InnerText; smslist.Add(smsentity); } ViewData["DataList"] = smslist; //return Json(smslist, JsonRequestBehavior.AllowGet); return View(); } catch (Exception ex) { throw ex; } finally { reader.Close(); } }

 

上面的代码是http://blog.csdn.net/u010028869/article/details/48899123

这个博客上的

我感觉解析的比我的好一点 

copy下来保存一下