Taylohtio/CustomNavigationProvider/CustomNavigationCommon/ConvertHelper.cs

175 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
namespace Taloyhtio.CustomNavigationCommon
{
public static class ConvertHelper
{
public static SiteMapNodeCollection BuildNodesFromXml(SiteMapProvider provider, XmlNode doc)
{
try
{
var collection = new SiteMapNodeCollection();
// foreach (XmlNode xmlNode in doc.ChildNodes)
// {
// if (xmlNode.Name == "node")
// {
// var node = new SiteMapNode(provider, xmlNode.Attributes["path"].Value,
// xmlNode.Attributes["url"].Value,
// xmlNode.Attributes["title"].Value);
//
// if (xmlNode.HasChildNodes)
// {
// var childNodes = new SiteMapNodeCollection();
// buildNodesFromXml(provider, xmlNode, childNodes);
// node.ChildNodes = childNodes;
// }
//
// collection.Add(node);
// }
// }
// when web service returns <root>...</root> xml document, it returns only internal content
// i.e. without root tag
if (doc.ChildNodes.Count == 1 && doc.ChildNodes[0].Name == Constants.TAG_ROOT)
{
doc = doc.ChildNodes[0];
}
buildNodesFromXml(provider, doc, collection);
return collection;
}
catch (Exception x)
{
return null;
}
}
private static void buildNodesFromXml(SiteMapProvider provider, XmlNode parentNode, SiteMapNodeCollection collection)
{
foreach (XmlNode xmlNode in parentNode.ChildNodes)
{
if (xmlNode.Name == Constants.TAG_NODE)
{
var node = new SiteMapNode(provider, xmlNode.Attributes[Constants.ATTR_PATH].Value,
xmlNode.Attributes[Constants.ATTR_URL].Value,
xmlNode.Attributes[Constants.ATTR_TITLE].Value);
if (xmlNode.HasChildNodes)
{
var childNodes = new SiteMapNodeCollection();
buildNodesFromXml(provider, xmlNode, childNodes);
node.ChildNodes = childNodes;
}
collection.Add(node);
}
}
}
public static XmlDocument BuildXmlFromMenuItem(MenuItemCollection collection)
{
if (collection == null || collection.Count == 0)
{
return null;
}
var doc = new XmlDocument();
var element = doc.CreateElement(Constants.TAG_ROOT);
doc.AppendChild(element);
foreach (MenuItem item in collection)
{
buildXmlFromMenuItem(item, doc, element);
}
return doc;
}
private static void buildXmlFromMenuItem(MenuItem item, XmlDocument doc, XmlNode xml)
{
if (item == null)
return;
XmlElement element = doc.CreateElement(Constants.TAG_NODE);
element.SetAttribute(Constants.ATTR_PATH, item.DataPath);
element.SetAttribute(Constants.ATTR_TITLE, item.Text);
element.SetAttribute(Constants.ATTR_URL, item.NavigateUrl);
xml.AppendChild(element);
foreach (MenuItem childItem in item.ChildItems)
{
buildXmlFromMenuItem(childItem, doc, element);
}
}
// private XmlDocument BuildXmlFromNode(SiteMapNode node)
// {
// XmlDocument doc = new XmlDocument();
//
// XmlElement element = doc.CreateElement("root");
// doc.AppendChild(element);
//
// BuildXmlFromNode(node, doc, element);
//
// return doc;
// }
//
// private XmlDocument BuildXmlFromNode(SiteMapNodeCollection collection)
// {
// XmlDocument doc = new XmlDocument();
//
// XmlElement element = doc.CreateElement("root");
// doc.AppendChild(element);
//
// if (collection == null)
// return doc;
//
// foreach (SiteMapNode node in collection)
// {
// BuildXmlFromNode(node, doc, element);
// }
//
// return doc;
// }
//
// private void BuildXmlFromNode(SiteMapNode node, XmlDocument doc, XmlNode xml)
// {
// if (node == null || !(node is PortalSiteMapNode))
// return;
// var portalNode = node as PortalSiteMapNode;
//
// XmlElement element = doc.CreateElement("node");
// element.SetAttribute("title", portalNode.Title);
// element.SetAttribute("url", portalNode.Url);
// element.SetAttribute("key", portalNode.Key);
//
// bool isVisible = true;
// try
// {
// isVisible = portalNode.IsVisible;
// }
// catch
// {
// }
// element.SetAttribute("visible", isVisible.ToString());
//
//
// xml.AppendChild(element);
//
// foreach (SiteMapNode childNode in node.ChildNodes)
// {
// BuildXmlFromNode(childNode, doc, element);
// }
// }
}
}