private static void WriteBranchNode(XmlTextWriter writer, BranchInfo branch, IList <string> teamProjects)
        {
            if (!teamProjects.Contains(branch.TeamProjectName))
            {
                writer.WriteStartElement("Node");
                writer.WriteAttributeString(PropertyIdId, branch.TeamProjectName);
                writer.WriteAttributeString(PropertyIdLabel, branch.TeamProjectName);
                writer.WriteAttributeString(PropertyIdCategory, CategoryIdTeamProject);
                writer.WriteAttributeString(PropertyIdGroup, "Collapsed");
                writer.WriteEndElement(); // </Node>
                teamProjects.Add(branch.TeamProjectName);
            }

            writer.WriteStartElement("Node");
            writer.WriteAttributeString(PropertyIdId, branch.Path);
            writer.WriteAttributeString(PropertyIdLabel, branch.Path);
            string category;

            if (branch.Parent == null && !branch.Children.Any())
            {
                category = CategoryIdOrphanBranch;
            }
            else if (branch.Parent == null)
            {
                category = CategoryIdRootBranch;
            }
            else if (!branch.Children.Any())
            {
                category = CategoryIdLeafBranch;
            }
            else
            {
                category = CategoryIdChildBranch;
            }
            writer.WriteAttributeString(PropertyIdCategory, category);
            if (!string.IsNullOrEmpty(branch.Description))
            {
                writer.WriteAttributeString(PropertyIdDescription, branch.Description);
            }
            writer.WriteAttributeString(PropertyIdDateCreated, branch.DateCreated.ToString("o", CultureInfo.InvariantCulture));
            if (!string.IsNullOrEmpty(branch.Owner))
            {
                writer.WriteAttributeString(PropertyIdOwner, branch.Owner);
            }
            if (!string.IsNullOrEmpty(branch.TeamProjectName))
            {
                writer.WriteAttributeString(PropertyIdTeamProject, branch.TeamProjectName);
            }
            writer.WriteAttributeString(PropertyIdBranchDepth, branch.BranchDepth.ToString());
            writer.WriteAttributeString(PropertyIdMaxTreeDepth, branch.MaxTreeDepth.ToString());
            writer.WriteEndElement(); // </Node>

            // Recursively write children.
            foreach (var child in branch.Children)
            {
                WriteBranchNode(writer, child, teamProjects);
            }
        }
        public BranchInfo(BranchInfo parent, string path, string description, DateTime dateCreated, string owner)
        {
            this.Parent      = parent;
            this.Path        = path;
            this.Description = description;
            this.DateCreated = dateCreated;
            this.Owner       = owner;

            this.DirectoryName = this.Path.Substring(this.Path.LastIndexOf('/') + 1);
            var secondSlashIndex = this.Path.IndexOf('/', 2);

            this.TeamProjectName = secondSlashIndex < 0 ? this.DirectoryName : this.Path.Substring(2, secondSlashIndex - 2);
            this.BranchDepth     = this.Parent == null ? 1 : this.Parent.BranchDepth + 1;
        }
        private static BranchInfo GetBranchInfo(BranchObject branch, BranchInfo parent, VersionControlServer vcs, ApplicationTask task)
        {
            var branchPath = branch.Properties.RootItem.Item;

            task.Status = "Processing " + branchPath;
            var current  = new BranchInfo(parent, branchPath, branch.Properties.Description, branch.DateCreated, branch.Properties.Owner);
            var children = new List <BranchInfo>();

            foreach (var childBranch in vcs.QueryBranchObjects(branch.Properties.RootItem, RecursionType.OneLevel).Where(c => c.Properties.RootItem.Item != branchPath).OrderBy(c => c.Properties.RootItem.Item))
            {
                if (task.IsCanceled)
                {
                    task.Status = "Canceled";
                    break;
                }
                children.Add(GetBranchInfo(childBranch, current, vcs, task));
            }
            current.Children = children.ToArray();
            return(current);
        }
        private static void WriteBranchLink(XmlTextWriter writer, BranchInfo branch)
        {
            // Link to the parent if it exists.
            if (branch.Parent != null)
            {
                writer.WriteStartElement("Link");
                writer.WriteAttributeString("Source", branch.Parent.Path);
                writer.WriteAttributeString("Target", branch.Path);
                writer.WriteEndElement(); // </Link>
            }

            // Link to the Team Project as a containment.
            writer.WriteStartElement("Link");
            writer.WriteAttributeString("Source", branch.TeamProjectName);
            writer.WriteAttributeString("Target", branch.Path);
            writer.WriteAttributeString("Category", "Contains");
            writer.WriteEndElement(); // </Link>

            // Recursively write children.
            foreach (var child in branch.Children)
            {
                WriteBranchLink(writer, child);
            }
        }