【发布时间】:2015-04-10 21:38:31
【问题描述】:
我要做的就是取出我的 MySite.sitemap 文件,从中吸出所有节点,然后将其打印到文件中。长期目标是获取每个节点并查看它是父节点还是子节点。
MySite.sitemap
<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="Grand Parent 1" roles="Grand Parent Role" description="A simple description that I might want to write to the text file.">
<siteMapNode title="Parent" description="" roles="Parent Role">
<siteMapNode title="Child 1" url="linkToPage.aspx" description="A short description." />
<siteMapNode title="Child 2" url="linkToPage2.aspx" description="Another short description." />
</siteMapNode>
<siteMapNode title="Parent 2" description="" roles="Parent Role" />
<siteMapNode title="Child 3" url="linkToPage3.aspx" description"A short description." />
<siteMapNode title="Child 4" url="linkToPage4.aspx" description="Another short description." />
</siteMapNode>
</siteMapNode>
</siteMap>
C#
StreamWriter file = new StreamWriter("debugFile.txt");
file.WriteLine("Sitemap File: " + pathToSiteMapFile + siteMapFile);
XmlDocument xml = new XmlDocument();
xml.Load(pathToSiteMapFile + siteMapFile);
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
//manager.AddNamespace("s", xml.DocumentElement.NamespaceURI);
XmlNodeList nodeList = xml.SelectNodes("/siteMap/siteMapNode", manager);
foreach (XmlNode node in nodeList) {
file.WriteLine("Trying to loop through xnlist...\r\n");
file.WriteLine("Node: " + node.InnerText + "\r\n");
}
foreach (string str in testPages) {
file.Write("File: " + str + "\r\n");
}
file.WriteLine("The foreach loop should have wrote all the nodes in here.\r\n");
file.Close();
如果有人能帮我弄清楚下一个问题是,一旦我枚举了每个节点,我是否能够进入每个节点并根据每个节点内的内容执行逻辑? IE 节点 a 具有节点 b 没有的角色。
【问题讨论】:
-
您想要所有的
siteMapNode节点还是只想要顶级节点? -
siteMapNode目前为止,但最好是全部。