public Nodes DeserializeFromString(string sNodesXml, ITreemapComponent oTreemapComponent) { Debug.Assert(sNodesXml != null); Debug.Assert(oTreemapComponent != null); AssertValid(); StringReader input = new StringReader(sNodesXml); XmlTextReader xmlTextReader = new XmlTextReader(input); xmlTextReader.WhitespaceHandling = WhitespaceHandling.Significant; Nodes nodes = null; try { xmlTextReader.IsStartElement("Nodes"); bool isEmptyElement = xmlTextReader.IsEmptyElement; nodes = new Nodes(null); DeserializeNodes(xmlTextReader, oTreemapComponent, nodes); if ((!isEmptyElement && xmlTextReader.NodeType != XmlNodeType.EndElement) || xmlTextReader.Name != "Nodes") { throw new ApplicationException(string.Format("The top-level {0} XML element is missing a closing element.", "Nodes")); } } catch (Exception innerException) { throw new ApplicationException("ITreemapComponent.NodesXml: The XML string is not in the expected format.", innerException); } finally { xmlTextReader.Close(); } return(nodes); }
public string SerializeToString(Nodes oNodes, ITreemapComponent oTreemapComponent) { Debug.Assert(oNodes != null); Debug.Assert(oTreemapComponent != null); AssertValid(); StringWriter stringWriter = new StringWriter(); XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); xmlTextWriter.Formatting = Formatting.Indented; SerializeNodes(oNodes, xmlTextWriter); xmlTextWriter.Close(); return(stringWriter.ToString()); }
protected Node DeserializeNode(XmlTextReader oXmlTextReader, ITreemapComponent oTreemapComponent) { Debug.Assert(oXmlTextReader != null); Debug.Assert(oXmlTextReader.NodeType == XmlNodeType.Element); Debug.Assert(oXmlTextReader.Name == "Node"); Debug.Assert(oTreemapComponent != null); this.AssertValid(); string text = SerializationUtil.DeserializeRequiredStringAttribute(oXmlTextReader, "Node", "Text"); float sizeMetric = SerializationUtil.DeserializeRequiredSingleAttribute(oXmlTextReader, "Node", "SizeMetric"); float colorMetric = SerializationUtil.DeserializeRequiredSingleAttribute(oXmlTextReader, "Node", "ColorMetric"); Color absoluteColor = Color.FromArgb(SerializationUtil.DeserializeRequiredInt32Attribute(oXmlTextReader, "Node", "AbsoluteColor")); string toolTip = SerializationUtil.DeserializeRequiredStringAttribute(oXmlTextReader, "Node", "ToolTip"); string attribute = oXmlTextReader.GetAttribute("Tag"); Node node = new Node(text, sizeMetric, colorMetric, attribute, toolTip); switch (oTreemapComponent.NodeColorAlgorithm) { case NodeColorAlgorithm.UseColorMetric: { break; } case NodeColorAlgorithm.UseAbsoluteColor: { node.AbsoluteColor = absoluteColor; break; } default: { Debug.Assert(false); break; } } if (!oXmlTextReader.Read() || oXmlTextReader.NodeType != XmlNodeType.Element || oXmlTextReader.Name != "Nodes") { throw new ApplicationException(string.Format("A {0} XML element is missing a required {1} child.", "Node", "Nodes")); } this.DeserializeNodes(oXmlTextReader, oTreemapComponent, node.Nodes); if (!oXmlTextReader.Read() || oXmlTextReader.NodeType != XmlNodeType.EndElement || oXmlTextReader.Name != "Node") { throw new ApplicationException(string.Format("A {0} XML element is missing a closing element.", "Node")); } return(node); }
protected void DeserializeNodes(XmlTextReader oXmlTextReader, ITreemapComponent oTreemapComponent, Nodes oNodes) { Debug.Assert(oXmlTextReader != null); Debug.Assert(oXmlTextReader.NodeType == XmlNodeType.Element); Debug.Assert(oXmlTextReader.Name == "Nodes"); Debug.Assert(oTreemapComponent != null); Debug.Assert(oNodes != null); AssertValid(); oNodes.EmptySpace.SizeMetric = SerializationUtil.DeserializeRequiredSingleAttribute(oXmlTextReader, "Nodes", "EmptySizeMetric"); if (oXmlTextReader.IsEmptyElement) { return; } while (oXmlTextReader.Read()) { switch (oXmlTextReader.NodeType) { case XmlNodeType.Element: if (oXmlTextReader.Name != "Node") { throw new ApplicationException(string.Format("A {0} XML element has an unexpected child node named {1}. Only {2} child nodes are allowed.", "Nodes", oXmlTextReader.Name, "Node")); } oNodes.Add(DeserializeNode(oXmlTextReader, oTreemapComponent)); if (oXmlTextReader.NodeType != XmlNodeType.EndElement || oXmlTextReader.Name != "Node") { throw new ApplicationException(string.Format("A {0} XML element is missing a closing element.", "Node")); } break; case XmlNodeType.EndElement: if (oXmlTextReader.Name != "Nodes") { throw new ApplicationException(string.Format("A {0} XML element is missing a closing element.", "Nodes")); } return; default: throw new ApplicationException(string.Format("A {0} XML element has an unexpected child node of type {1}. Only {2} child nodes are allowed.", "Nodes", oXmlTextReader.NodeType, "Node")); } } }