Пример #1
0
        public static List <CategoryElement> GetElementsFromFile(string filePath, string mainItem, string subItem)
        {
            MainCategory currentCategory = new MainCategory("root", "", null);
            int          nestedDepth     = 0;

            using (StreamReader sr = new StreamReader(filePath))
            {
                using (XmlReader reader = XmlReader.Create(sr))
                {
                    reader.Read();
                    reader.Read();
                    reader.Read();
                    while (reader.NodeType != XmlNodeType.EndElement || reader.Name != mainItem)
                    {
                        if (reader.NodeType == XmlNodeType.EndElement && reader.Name == subItem)
                        {
                            nestedDepth--;
                            currentCategory = currentCategory.ParentElement;
                        }

                        if (reader.NodeType == XmlNodeType.Element && reader.Name == subItem)
                        {
                            nestedDepth++;

                            MainCategory category = new MainCategory(reader.GetAttribute("Name"), reader.GetAttribute("ID"), currentCategory);
                            currentCategory.AddElement(category);
                            currentCategory = category;
                        }
                        else if (reader.NodeType == XmlNodeType.Element && reader.Name == "Topic")
                        {
                            reader.Read();
                            if (reader.NodeType == XmlNodeType.Text)
                            {
                                Topic topic = new Topic(reader.Value);
                                currentCategory.AddElement(topic);
                            }
                        }

                        reader.Read();
                    }
                }
            }
            return(currentCategory.Elements);
        }
Пример #2
0
 public MainCategory(string title, string id, MainCategory parent) : base(title, id)
 {
     Elements      = new List <CategoryElement>();
     ParentElement = parent;
 }