将XML站点地图解析为分层数据对象?

时间:2009-06-09 22:39:23

标签: c# xml

有没有快速的方法将XML站点地图放入分层数据对象?

或者也许我从一开始就会犯这个错误。我想合并两个XML文件。

第一个是站点地图:

<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>

第二个是更多只是用于构建菜单的数据存储:

<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>

我想只维护一个文件。

2 个答案:

答案 0 :(得分:0)

可以想象,您可以创建包含其中所有数据的混合文件,并使用XSLT在构建过程或运行时(如果适用)创建数据的“视图”。

答案 1 :(得分:0)

这是我最终做的事情:

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;
        }
}

菜单类:

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>();
}