private List <TableOfContentItem> GetTableOfContentItems(string XmlFile, string StartingElement) { List <TableOfContentItem> tocItems = new List <TableOfContentItem>(); using (XmlReader reader = XmlReader.Create(XmlFile)) { reader.MoveToContent(); bool splittingStarted = (StartingElement == null); string elementName = null; string actualPageBreak = null; string actualPageBreakXmlId = null; string actualDivId = null; string actualEntryType = null; TableOfContentItem currentHwItem = null; ItemBase currentTocItem = null; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { elementName = reader.Name; bool isEmpty = reader.IsEmptyElement; if (!splittingStarted) { if (reader.Name == StartingElement) { splittingStarted = true; } } if (!splittingStarted) { continue; } switch (elementName) { case "pb": actualPageBreak = reader.GetAttribute("n"); actualPageBreakXmlId = reader.GetAttribute("id", XmlNamespace); break; case "div": actualDivId = reader.GetAttribute("id", XmlNamespace); break; case "head": ItemBase parent = currentHwItem; if (parent != null && (parent.Level == reader.Depth || parent.DivXmlId == actualDivId)) { parent = currentHwItem.Parent; } TableOfContentItem tocItem = new TableOfContentItem(parent); tocItem.Level = reader.Depth; GetElementHead(reader, tocItem); tocItem.PageBreakInfo.PageBreak = actualPageBreak; tocItem.PageBreakInfo.PageBreakXmlId = actualPageBreakXmlId; tocItem.DivXmlId = actualDivId; if (currentHwItem == null) { tocItems.Add(tocItem); } else { if (currentHwItem.Parent == null) { if (currentHwItem.Level < tocItem.Level || currentHwItem.DivXmlId == tocItem.DivXmlId) { currentHwItem.Sections.Add(tocItem); } else { tocItems.Add(tocItem); } } else { if (currentHwItem.DivXmlId == tocItem.DivXmlId && currentHwItem.Parent != null && currentHwItem.Parent.DivXmlId == tocItem.DivXmlId) { currentHwItem.Parent.Sections.Add(tocItem); } else { if (currentHwItem.Level == tocItem.Level) { currentHwItem.Parent.Sections.Add(tocItem); } else { currentHwItem.Sections.Add(tocItem); } } } } currentHwItem = tocItem; break; } } else if (reader.NodeType == XmlNodeType.EndElement) { if (elementName == "div") { actualDivId = null; if (currentHwItem != null) { currentHwItem = currentHwItem.Parent as TableOfContentItem; } } } } } return(tocItems); }
public TableOfContentItem(ItemBase parent) : base(parent) { Sections = new List <TableOfContentItem>(); }
protected List <HeadwordsListItem> GetHeadwordsListItems(string XmlFile, string StartingElement) { List <HeadwordsListItem> headwords = new List <HeadwordsListItem>(); bool isFormUsed = false; using (XmlReader reader = XmlReader.Create(XmlFile)) { reader.MoveToContent(); bool splittingStarted = (StartingElement == null); string elementName = null; string actualPageBreak = null; string actualPageBreakXmlId = null; string actualDivId = null; string actualEntryType = null; HeadwordsListItem currentHwItem = null; ItemBase currentTocItem = null; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { elementName = reader.Name; if (!splittingStarted) { if (reader.Name == StartingElement) { splittingStarted = true; } } if (!splittingStarted) { continue; } switch (elementName) { case "pb": actualPageBreak = reader.GetAttribute("n"); actualPageBreakXmlId = reader.GetAttribute("id", XmlNamespace); break; case "entryFree": actualEntryType = reader.GetAttribute("type"); goto case "div"; case "div": actualDivId = reader.GetAttribute("id", XmlNamespace); break; case "form": isFormUsed = true; goto case "head"; case "head": HeadwordsListItem parent = currentHwItem; if (parent != null && (parent.Level == reader.Depth || parent.DivXmlId == actualDivId)) { parent = currentHwItem.Parent as HeadwordsListItem; } HeadwordsListItem tocItem = new HeadwordsListItem(parent); tocItem.HeadwordInfo.FormXmlId = reader.GetAttribute("id", XmlNamespace); tocItem.HeadwordInfo.Type = reader.GetAttribute("type"); tocItem.Level = reader.Depth; GetElementHead(reader, tocItem); tocItem.PageBreakInfo.PageBreak = actualPageBreak; tocItem.PageBreakInfo.PageBreakXmlId = actualPageBreakXmlId; tocItem.DivXmlId = actualDivId; if (currentHwItem == null) { headwords.Add(tocItem); } else { if (elementName == "form" && currentHwItem.DivXmlId != tocItem.DivXmlId) { HeadwordsListItem tocEntry = new HeadwordsListItem(parent); tocEntry.DivXmlId = actualDivId; tocEntry.HeadInfo.HeadText = tocItem.HeadInfo.HeadText; tocEntry.Level = tocItem.Level; tocEntry.HeadwordInfo.Type = actualEntryType; parent.Sections.Add(tocEntry); tocItem.Parent = tocEntry; currentHwItem = tocEntry; } if (currentHwItem.Parent == null) { if (currentHwItem.Level < tocItem.Level || currentHwItem.DivXmlId == tocItem.DivXmlId) { currentHwItem.Sections.Add(tocItem); } else { headwords.Add(tocItem); } } else { if (currentHwItem.DivXmlId == tocItem.DivXmlId && currentHwItem.Parent != null && currentHwItem.Parent.DivXmlId == tocItem.DivXmlId) { currentHwItem.Parent.Sections.Add(tocItem); } else { if (elementName == "head" && currentHwItem.Level == tocItem.Level) { currentHwItem.Parent.Sections.Add(tocItem); } else { currentHwItem.Sections.Add(tocItem); } } } } if (elementName != "form") { currentHwItem = tocItem; } break; } } else if (reader.NodeType == XmlNodeType.EndElement) { if (elementName == "div") { actualDivId = null; if (currentHwItem != null) { currentHwItem = currentHwItem.Parent as HeadwordsListItem; } } } } } if (!isFormUsed) { headwords = new List <HeadwordsListItem>(); } return(headwords); }