/// <summary> /// Loads a <see cref = "IBDocument" /> from a Xml content. /// </summary> /// <param name = "reader">The reader.</param> /// <returns>An instance of <see cref = "IBDocument" /></returns> public static IBDocument LoadFromXml(XmlReader reader) { IBDocument document = new IBDocument(); IIBReferenceResolver resolver = new IBReferenceResolver(); // The XML parsing is based on a top-down stack exploration StringBuilder tempContent = new StringBuilder(); Stack<IIBItem> stack = new Stack<IIBItem>(); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: { String name = reader.Name; bool empty = reader.IsEmptyElement; // Collect all the attributes Dictionary<String, String> attributes = new Dictionary<String, String>(); if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); attributes.Add(reader.Name, reader.Value); } } // Create an object corresponding to the node IIBItem item = Create(name, attributes); if (String.Equals("archive", name)) { // The "archive" node has a special treatment document.Root = item as IBArchive; } else { // Append the node to the current parent node IIBItem current = stack.Peek(); current.AppendChild(item); } // If the node has an id, then add it to the resolver for latter processing resolver.AddReference(item); // Push the new node in the context stack stack.Push(item); // Prepare the content collector for node with text tempContent = new StringBuilder(); // An empty node is pop right-away from the context stack if (empty) { stack.Pop(); item.SetValue(tempContent.ToString()); item.Finish(resolver); } } break; case XmlNodeType.EndElement: { // When the node ends, it is pop from the context stack and its content is set // After that, the node can perform additionnal processing. IIBItem item = stack.Pop(); item.SetValue(tempContent.ToString()); item.Finish(resolver); } break; case XmlNodeType.Text: case XmlNodeType.CDATA: case XmlNodeType.SignificantWhitespace: { // Collect text content between start and end of the node String value = reader.Value; tempContent.Append(value); } break; } } return document; }
/// <summary> /// Loads a <see cref = "IBDocument" /> from a Xml content. /// </summary> /// <param name = "reader">The reader.</param> /// <returns>An instance of <see cref = "IBDocument" /></returns> public static IBDocument LoadFromXml(XmlReader reader) { IBDocument document = new IBDocument(); IIBReferenceResolver resolver = new IBReferenceResolver(); // The XML parsing is based on a top-down stack exploration StringBuilder tempContent = new StringBuilder(); Stack <IIBItem> stack = new Stack <IIBItem>(); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: { String name = reader.Name; bool empty = reader.IsEmptyElement; // Collect all the attributes Dictionary <String, String> attributes = new Dictionary <String, String>(); if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); attributes.Add(reader.Name, reader.Value); } } // Create an object corresponding to the node IIBItem item = Create(name, attributes); if (String.Equals("archive", name)) { // The "archive" node has a special treatment document.Root = item as IBArchive; } else { // Append the node to the current parent node IIBItem current = stack.Peek(); current.AppendChild(item); } // If the node has an id, then add it to the resolver for latter processing resolver.AddReference(item); // Push the new node in the context stack stack.Push(item); // Prepare the content collector for node with text tempContent = new StringBuilder(); // An empty node is pop right-away from the context stack if (empty) { stack.Pop(); item.SetValue(tempContent.ToString()); item.Finish(resolver); } } break; case XmlNodeType.EndElement: { // When the node ends, it is pop from the context stack and its content is set // After that, the node can perform additionnal processing. IIBItem item = stack.Pop(); item.SetValue(tempContent.ToString()); item.Finish(resolver); } break; case XmlNodeType.Text: case XmlNodeType.CDATA: case XmlNodeType.SignificantWhitespace: { // Collect text content between start and end of the node String value = reader.Value; tempContent.Append(value); } break; } } return(document); }
private static void CheckDocument(IBDocument document) { Assert.IsNotNull(document.Root); }