public static XmlDocument CreateTreeXmlDocument(this IEnumerable <TreeData> objTreeDataList, int rootParentID, string navigateUrl, string IDParName, string ParentIDParName, Action <TreeData, XmlElement> objAction) { List <TreeData> source = (from s in objTreeDataList where s.ParentID == rootParentID select s).ToList <TreeData>(); TreeData data = null; if (source.Count > 1) { data = new TreeData { ParentID = -1, TreeNodeName = "节点管理", TreeNodeID = rootParentID }; } else { data = source.FirstOrDefault <TreeData>(); } if (data == null) { throw new ArgumentNullException("找不到相应的:rootParentID=" + rootParentID + "子节点"); } XmlDocument xmlDocSource = new XmlDocument(); string str = ((navigateUrl.IndexOf("?") >= 0) ? (navigateUrl + "&") : (navigateUrl + "?")) + (IDParName.IsNull() ? "TransferID" : IDParName) + "={0}&" + (ParentIDParName.IsNull() ? "TransferTwoID" : ParentIDParName) + "={1}&IsChild={2}"; XmlElement element = xmlDocSource.CreateElement("TreeNodeMember"); if (objAction != null) { objAction(data, element); } else { element.SetAttribute("TreeNodeID", data.TreeNodeID.ToString()); element.SetAttribute("TreeNodeName", data.TreeNodeName); if (!string.IsNullOrWhiteSpace(str)) { element.SetAttribute("NavigateUrl", string.Format(str, data.TreeNodeID.ToString(), data.ParentID, 1).EncryptModuleQuery()); } } CreateTreeXmlElement(xmlDocSource, data.TreeNodeID, element, objTreeDataList, str, objAction); xmlDocSource.AppendChild(element); return(xmlDocSource); }
private static void CreateTreeXmlElement(XmlDocument xmlDocSource, int ParentID, XmlElement objXmlElement, IEnumerable <TreeData> objTreeDataList, string urlformat, Action <TreeData, XmlElement> objAction) { using (IEnumerator <TreeData> enumerator = (from s in objTreeDataList where s.ParentID == ParentID orderby s.SortIndex select s).GetEnumerator()) { while (enumerator.MoveNext()) { Func <TreeData, bool> predicate = null; TreeData objTreeData = enumerator.Current; XmlElement element = xmlDocSource.CreateElement("TreeNodeMember"); if (objAction != null) { objAction(objTreeData, element); objXmlElement.AppendChild(element); } else { if (predicate == null) { predicate = s => s.ParentID == objTreeData.TreeNodeID; } int num = objTreeDataList.Any <TreeData>(predicate) ? 1 : 0; element.SetAttribute("TreeNodeID", objTreeData.TreeNodeID.ToString()); element.SetAttribute("TreeNodeName", objTreeData.TreeNodeName); if (!string.IsNullOrWhiteSpace(urlformat)) { element.SetAttribute("NavigateUrl", string.Format(urlformat, objTreeData.TreeNodeID.ToString(), objTreeData.ParentID, num).EncryptModuleQuery()); } objXmlElement.AppendChild(element); } CreateTreeXmlElement(xmlDocSource, objTreeData.TreeNodeID, element, objTreeDataList, urlformat, objAction); } } }