using System.Xml; namespace Murli_ConsoleApplication1 { class Program { static void Main( string[] args ) { // Load xml document. #region Load Xml in Document or Dom string strFileName = Server.MapPath("~/RootLinks/Navigation.xml"); oXmlDoc = new XmlDocument(); try { oXmlDoc.Load(strFileName); } catch (Exception ex) { //Console.Write("Error: " + ex.Message); } //XmlNode oNode = oXmlDoc.DocumentElement; #endregion TraverseNodes(oXmlDoc.ChildNodes); } private static void TraverseNodes(XmlNodeList nodes ) { foreach( XmlNode node in nodes ) { // Do something with the node. TraverseNodes( node.ChildNodes ); } } } }
Realtime Example
public static string GetDynamicFileName(this string fName, XmlNode xml) { //folder="True" bool IsCMSView = true; string FileName = xml.Attributes["filename"].Value; string PageId = xml.Attributes["id"].Value; string CompleteURL = fName; if (xml.Attributes["real"].Value == "0") { string strFileName = HttpContext.Current.Server.MapPath("~/RootLinks/Navigation.xml"); XmlDocument oXmlDoc = new XmlDocument(); try { oXmlDoc.Load(strFileName); } catch (Exception ex) { //lblMessage.Text = "Error: " + ex.Message; //Response.Write("Error: " + ex.Message); } XmlNodeList xx = oXmlDoc.SelectNodes("/nav/page"); //Or //XmlNodeList xx = oXmlDoc.ChildNodes; TraverseNodes(xx, ref FileName, ref PageId); } } private static void TraverseNodes(XmlNodeList nodes, ref string FileName,ref string PageId) { foreach (XmlNode node in nodes) { if (node.Attributes["id"].Value == FileName) { FileName = node.Attributes["filename"].Value; PageId = node.Attributes["id"].Value; break; } // Do something with the node. TraverseNodes(node.ChildNodes, ref FileName, ref PageId); } }
No comments:
Post a Comment