递归方法导致无限循环

时间:2013-11-22 09:03:16

标签: c# recursion sharepoint-2010 migration infinite-loop

我正在为sharepoint网站做一个迁移工具。我使用XML文件迁移网站。然而,在我生成XML文件的方法中,我得到了一个无限循环,我无法弄清楚原因。我没有得到任何错误,但XML文件变得可以理解的大。

我如何创建XML文件的根节点:

private void createRootXmlNodeFromRootSite()
        {
            if (rootsite != null)
            {
                //Add root-site to TreeView
                XmlElement rootnode = document.CreateElement(strRoot);
                rootnode.SetAttribute(strTitle, rootsite.Title);
                rootnode.SetAttribute(strName, rootsite.SiteAdministrators[0].Name);
                rootnode.SetAttribute(strEmail, rootsite.SiteAdministrators[0].Email);
                document.AppendChild(rootnode);

                XmlElement nodelists = document.CreateElement(strLists);

                if (rootsite.Lists.Count > 0)
                {
                    rootnode.AppendChild(nodelists);

                    //Get all lists within root-site and put them under it's node.
                    foreach (SPList list in rootsite.Lists)
                    {
                        XmlElement nodelist = document.CreateElement(strList);
                        nodelist.SetAttribute(strTitle, list.Title);

                        if (!list.Hidden)
                        {
                            nodelists.AppendChild(nodelist);

                            XmlElement nodects = document.CreateElement(strContentTypes);

                            if (list.ContentTypes.Count > 0)
                            {
                                nodelist.AppendChild(nodects);

                                //Get all content types within lists of root site and put them under it's node.
                                foreach (SPContentType ct in list.ContentTypes)
                                {
                                    XmlElement nodect = document.CreateElement(strContentType);
                                    nodect.SetAttribute(strTitle, ct.Name);

                                    if (!ct.Hidden)
                                    {
                                        nodects.AppendChild(nodect);

                                        XmlElement nodefields = document.CreateElement(strFields);

                                        if (ct.Fields.Count > 0)
                                        {
                                            nodect.AppendChild(nodefields);

                                            foreach (SPField field in ct.Fields)
                                            {
                                                XmlElement nodefield = document.CreateElement(strField);
                                                nodefield.SetAttribute(strTitle, field.Title);

                                                if (!field.Hidden)
                                                {
                                                    nodefields.AppendChild(nodefield);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        //Call recursive methods to get child-site-elements.
                        convertSiteCollectionToXml(rootnode, rootsite);
                    }
                }
            }
        }

然后,此方法调用一个递归方法,该方法从子网站创建子节点:

private void convertSiteCollectionToXml(XmlNode nodeElement, SPWeb site)
        {
            XmlElement nodesites = document.CreateElement("Sites");

            if (site.Webs.Count > 0)
            {
                nodeElement.AppendChild(nodesites);

                foreach (SPWeb childsite in site.Webs)
                {
                    XmlElement child = document.CreateElement("Site");
                    child.SetAttribute("Title", childsite.Title);
                    nodesites.AppendChild(child);

                    XmlElement nodelists = document.CreateElement("Lists");

                    if (childsite.Lists.Count > 0)
                    {
                        child.AppendChild(nodelists);

                        foreach (SPList list in childsite.Lists)
                        {
                            XmlElement nodelist = document.CreateElement("List");
                            nodelist.SetAttribute("Title", list.Title);

                            if (!list.Hidden)
                            {
                                nodelists.AppendChild(nodelist);

                                XmlElement nodects = document.CreateElement("ContentTypes");

                                if (list.ContentTypes.Count > 0)
                                {
                                    nodelist.AppendChild(nodects);

                                    foreach (SPContentType ct in list.ContentTypes)
                                    {
                                        XmlElement nodect = document.CreateElement("ContentType");
                                        nodect.SetAttribute("Title", ct.Name);

                                        if (!ct.Hidden)
                                        {
                                            nodects.AppendChild(nodect);

                                            XmlElement nodefields = document.CreateElement("Fields");

                                            if (ct.Fields.Count > 0)
                                            {
                                                nodect.AppendChild(nodefields);

                                                foreach (SPField field in ct.Fields)
                                                {
                                                    XmlElement nodefield = document.CreateElement("Field");
                                                    nodefield.SetAttribute("Title", field.Title);

                                                    if (!field.Hidden)
                                                    {
                                                        nodefields.AppendChild(nodefield);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            convertSiteCollectionToXml(child, childsite);
                        }
                    }
                }
            }
        }

无限循环发生在第二种方法中。有人看到原因吗?

0 个答案:

没有答案