tags:

views:

364

answers:

2

Is there a quick way to get a XML sitemap into a hierarchical data object?

Or maybe I'm going about this wrong from the get go. I would like to combine two XML files.

The first on is a sitemap:

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
   <siteMapNode description="Pages" title="Pages">
     <siteMapNode target="_blank" url="~/1.aspx" title="1" description="1"/>
   </siteMapNode>
   <siteMapNode  target="_blank" url="~/2.aspx" title="2" description="2" >
     <siteMapNode target="_blank" url="~/3.aspx" title="3" description="3"/>
     <siteMapNode target="_blank" url="~/4.aspx" title="4" description="4"/>
   </siteMapNode>
</siteMap>

The second is is more of just a data store that is used to build a menu:

<root>
   <item ID="BB" InnerHtml="Group1"/>
   <item ID="BB1" ParentID="BB" InnerHtml="1" Url="~/1.aspx" UrlTarget="_Blank"/>
   <item ID="BB2" ParentID="BB" InnerHtml="2" Url="~/2.aspx" UrlTarget="_Blank"/>
   <item ID="BB3" ParentID="BB" InnerHtml="3" Url="~/3.aspx" UrlTarget="_Blank"/>
   <item ID="BD" InnerHtml="Group2"/>
   <item ID="BD1" ParentID="BD" InnerHtml="4" Url="~/4.aspx" UrlTarget="_Blank"/>
   <item ID="BD2" ParentID="BD" InnerHtml="5" Url="~/5.aspx" UrlTarget="_Blank"/>
   <item ID="BD3" ParentID="BD" InnerHtml="6" Url="~/6.aspx" UrlTarget="_Blank"/>
</root>

I would like to only have one file to maintain.

A: 

Conceivably, you could create a hybrid file with all the data in it and use XSLT to create 'views' of the data during your build process or runtime where applicable.

steamer25
A: 

Here is what I ended up doing:

private const string XmlFileNameKey = "xmlMenuFileName";
private const string XmlItemTag = "siteMapNode";
private const string XmlNodeTarget = "target";

Public MenuItem ReadXML()
{
        MenuItem retVal = new MenuItem(null);
        XmlTextReader reader = new XmlTextReader(xmlFileLocation);
        retVal.topMost = true;

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Attribute:
                    break;
                case XmlNodeType.CDATA:
                    break;
                case XmlNodeType.Comment:
                    break;
                case XmlNodeType.Document:
                    break;
                case XmlNodeType.DocumentFragment:
                    break;
                case XmlNodeType.DocumentType:
                    break;
                case XmlNodeType.Element:
                    if (reader.Name == XmlItemTag)
                    {
                        bool isempty = reader.IsEmptyElement;

                        MenuItem temp = new MenuItem();
                        temp.topMost = false;
                        int attributeCount = reader.AttributeCount;
                        if (attributeCount > 0)
                        {
                            for (int i = 0; i < attributeCount; i++)
                            {
                                reader.MoveToAttribute(i);
                                SetAttributeValue(ref temp, reader.Name, reader.Value);
                            }
                        }
                       retVal.children.Add(temp);
                        if (!isempty)
                        {
                            temp.parent = retVal;
                             retVal = temp;    
                        }

                    }
                    break;
                case XmlNodeType.EndElement:
                   string test = reader.Name;
                   if (retVal.parent != null )
                   {
                        retVal = retVal.parent;
                   }

                    break;
                case XmlNodeType.EndEntity:
                    break;
                case XmlNodeType.Entity:
                    break;
                case XmlNodeType.EntityReference:
                    break;
                case XmlNodeType.None:
                    break;
                case XmlNodeType.Notation:
                    break;
                case XmlNodeType.ProcessingInstruction:
                    break;
                case XmlNodeType.SignificantWhitespace:
                    break;
                case XmlNodeType.Text:
                    break;
                case XmlNodeType.Whitespace:
                    break;
                case XmlNodeType.XmlDeclaration:
                    break;
                default:
                    break;
            }
        }

        //Close the reader
        reader.Close();
        while (retVal.parent != null && !retVal.topMost)
        {
            retVal = retVal.parent;
        }
}

The menu class:

public bool topMost = false;
public string description;
public string title;
public string url;
public string target;
public List<MenuItem> children;
public MenuItem parent;

public MenuItem(MenuItem parent)
{
    children = new List<MenuItem>();
    if (parent != null)
    {
        this.parent = parent;
    }
}

public MenuItem()
{
    children = new List<MenuItem>();
}
NitroxDM