using System; using System.Collections.Generic; using System.Xml; namespace Svg { internal sealed class SvgNodeReader : XmlNodeReader { private readonly Dictionary _entities; private string _value; private bool _customValue = false; public SvgNodeReader(XmlNode node, Dictionary entities) : base(node) { _entities = entities ?? new Dictionary(); } /// /// Gets the text value of the current node. /// /// /// The value returned depends on the of the node. The following table lists node types that have a value to return. All other node types return String.Empty.Node Type Value AttributeThe value of the attribute. CDATAThe content of the CDATA section. CommentThe content of the comment. DocumentTypeThe internal subset. ProcessingInstructionThe entire content, excluding the target. SignificantWhitespaceThe white space within an xml:space= 'preserve' scope. TextThe content of the text node. WhitespaceThe white space between markup. XmlDeclarationThe content of the declaration. public override string Value { get { return _customValue ? _value : base.Value; } } /// /// Reads the attribute value. /// /// /// true if there is a attribute value; false if there are no attribute value. /// public override bool ReadAttributeValue() { _customValue = false; var read = base.ReadAttributeValue(); if (read && NodeType == XmlNodeType.EntityReference) ResolveEntity(); return read; } /// /// Reads the next node from the stream. /// /// /// true if the next node was read successfully; false if there are no more nodes to read. /// /// An error occurred while parsing the XML. public override bool Read() { _customValue = false; bool read = base.Read(); if (NodeType == XmlNodeType.DocumentType) ParseEntities(); return read; } private void ParseEntities() { const string entityText = " /// Resolves the entity reference for EntityReference nodes. /// public override void ResolveEntity() { if (NodeType == XmlNodeType.EntityReference) { if (_entities.ContainsKey(Name)) { _value = _entities[Name]; } else { base.ResolveEntity(); _value = ReadAttributeValue() ? base.Value : string.Empty; } _customValue = true; } } } }