public override void MoveToAttribute(int i) { // forward to reader if not replaying if (curNode == null) { reader.MoveToAttribute(i); return; } // check that there are some cached attributes if (curAttrParent != null && curAttrParent.attributes != null) { // iterate through attributes fo find one with the given index CachedXmlNode attr = curAttrParent.attributes; int index = 0; do { if (i == index) { curNode = attr; return; } attr = attr.next; index++; } while (attr != null); } // not found -> invalid index throw new ArgumentOutOfRangeException("i"); }
// // Bookmarking methods // // Sets a bookmark with the given name on the current node. public void SetBookmark(string bookmarkName) { if (reader.ReadState != ReadState.Interactive) { throw new InvalidOperationException("A bookmark can be set only when the reader is in ReadState.Interactive."); } if (reader.NodeType == XmlNodeType.Attribute) { throw new InvalidOperationException("A bookmark cannot be set when the reader on an attribute"); } // check that the bookmark name is unique if (bookmarks[bookmarkName] != null) { throw new ArgumentException("Duplicate bookmark name.", "bookmarkName"); } // figure out the first node of the bookmark, start caching if we are not already doing that CachedXmlNode bookmarkNode = curNode; if (curNode == null) { if (cachedNodes != null) { bookmarkNode = cachedNodes; } else { Debug.Assert(bookmarks.Count == 0, "There should be no bookmarks and no cached nodes."); CacheCurrentNode(); bookmarkNode = cachedNodes; } } // create a bookmark bookmarks.Add(bookmarkName, bookmarkNode); }
public override bool MoveToAttribute(string localName, string namespaceUri) { // forward to reader if not replaying if (curNode == null) { return(reader.MoveToAttribute(localName, namespaceUri)); } // check that there are some cached attributes if (curAttrParent != null && curAttrParent.attributes != null) { // atomize names localName = reader.NameTable.Get(localName); namespaceUri = reader.NameTable.Get(namespaceUri); // if the name is not in name table, there is no attribute with such name if (localName == null || namespaceUri == null) { return(false); } // iterate through attributes fo find one with the given name CachedXmlNode attr = curAttrParent.attributes; do { if ((object)localName == (object)attr.localName && (object)namespaceUri == (object)attr.namespaceUri) { curNode = attr; return(true); } attr = attr.next; } while (attr != null); } // attribute not found return(false); }
public override string GetAttribute(int i) { // forward to base reader if replaying if (curNode == null) { return(reader.GetAttribute(i)); } // check that there are some cached attributes if (curAttrParent != null && curAttrParent.attributes != null) { // iterate through attributes fo find one with the given index CachedXmlNode attr = curAttrParent.attributes; int index = 0; do { if (i == index) { // found one -> return value return(attr.value); } attr = attr.next; index++; } while (attr != null); } throw new ArgumentOutOfRangeException("i"); }
public override string GetAttribute(string name) { // forward to base reader if replaying if (curNode == null) { return(reader.GetAttribute(name)); } // check that there are some cached attributes if (curAttrParent != null && curAttrParent.attributes != null) { // atomize the name name = reader.NameTable.Get(name); // if the name is not in name table, there is no attribute with such name -> return if (name == null) { return(null); } // iterate through attributes fo find the one with the given name CachedXmlNode attr = curAttrParent.attributes; do { if ((object)name == (object)attr.name) { // found one -> return value return(attr.value); } attr = attr.next; } while (attr != null); } // attribute not found return(null); }
public override bool ReadAttributeValue() { // forward to base reader if replaying if (curNode == null) { return(reader.ReadAttributeValue()); } // return false on node type type other than attribute if (curNode.nodeType != XmlNodeType.Attribute) { return(false); } // setup a cached node for attribute value if (attributeTextValue == null) { attributeTextValue = new CachedXmlNode(XmlNodeType.Text, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, 0, null, null); } attributeTextValue.value = curNode.value; attributeTextValue.depth = curNode.depth + 1; attributeTextValue.namespacesInScope = currentNamespacesInScope; attributeTextValue.next = curNode; curNode = attributeTextValue; return(true); }
// Removes a bookmark with the given name. The XML nodes cached for this bookmark will be released // for garbage collection unless they are needed by a preceding bookmark. public void RemoveBookmark(string bookmarkName) { bookmarks.Remove(bookmarkName); if (bookmarks.Count == 0) { cachedNodes = null; } }
internal CachedXmlNode(XmlNodeType nodeType, string name, string localName, string prefix, string namespaceUri, string value, int depth, NamespaceDecl namespacesInScope, CachedXmlNode next) { this.nodeType = nodeType; this.name = name; this.localName = localName; this.prefix = prefix; this.namespaceUri = namespaceUri; this.value = value; this.depth = depth; this.namespacesInScope = namespacesInScope; this.next = next; isDefaultOrEmpty = false; this.attributes = null; }
public override bool MoveToFirstAttribute() { // forward to base reader if not replaying if (curNode == null) { return(reader.MoveToFirstAttribute()); } // return false if there are no attributes if (curAttrParent == null || curAttrParent.attributes == null) { return(false); } // move to the first attribute curNode = curAttrParent.attributes; return(true); }
public override bool Read() { // have current node -> we are replaying if (curNode != null) { // recover from iterating over attributes if (curAttrParent != null) { curNode = curAttrParent; if (attributeTextValue != null) { attributeTextValue.next = null; } } // move to next node in the list if (curNode.next != null) { SetCurrentNode(curNode.next); return(true); } // end of cached nodes else { curNode = null; } } // pop namespace scope if previous node was an end element or an empty element currentNamespacesInScope = nextNamespacesInScope; // read next node from the reader if (!reader.Read()) { return(false); } // save namespaces ProcessNamespaces(); // have some bookmark need to cache the node if (bookmarks.Count > 0) { CacheCurrentNode(); } return(true); }
public override bool MoveToElement() { // forward to base reader if replaying if (curNode == null) { reader.MoveToElement(); } // move to attribute parent if (curAttrParent != null) { if (curNode != curAttrParent) { curNode = curAttrParent; return(true); } } return(false); }
public override bool MoveToNextAttribute() { // forward to base reader if not replaying if (curNode == null) { return(reader.MoveToNextAttribute()); } // return false if there are no attributes if (curAttrParent == null || curAttrParent.attributes == null) { return(false); } if (curNode.nodeType != XmlNodeType.Attribute) { // if on attribute parent, move to the first attribute if (curNode == curAttrParent) { if (curAttrParent.attributes != null) { curNode = curAttrParent.attributes; return(true); } } // if on attribute text value, move to the next attribute else if (curNode == attributeTextValue) { CachedXmlNode nextAttr = attributeTextValue.next.next; if (nextAttr != null) { curNode = nextAttr; attributeTextValue.next = null; return(true); } } return(false); } // otherwise move to the next attribute, if one exists if (curNode.next != null) { curNode = curNode.next; return(true); } return(false); }
void ReturnToBookmark(string bookmarkName, bool remove) { // find the bookmark CachedXmlNode bookmarkedNode = (CachedXmlNode)bookmarks[bookmarkName]; while (bookmarkedNode != null) { // found it -> restart the reader to replay from this point SetCurrentNode(bookmarkedNode); // remove it from the list of bookmarks if (remove) { bookmarks.Remove(bookmarkName); } cachedNodes = null; return; } throw new ArgumentException("Bookmark \"" + bookmarkName + "\" does not exist.", "bookmarkName"); }
// // Private implementation methods // // Copies the properties of the current XmlReader node into a CachedXmlNode record and adds it to the list of cachedNodes private void CacheCurrentNode() { XmlNodeType nt = reader.NodeType; // cache the node CachedXmlNode node = new CachedXmlNode(reader, currentNamespacesInScope, null); if (nt == XmlNodeType.Element) { node.isDefaultOrEmpty = reader.IsEmptyElement; } if (cachedNodes != null) { cachedNodes.next = node; } cachedNodes = node; // cache its attributes if (reader.MoveToFirstAttribute()) { CachedXmlNode lastAttr = null; do { CachedXmlNode attr = new CachedXmlNode(reader, currentNamespacesInScope, null); if (lastAttr == null) { node.attributes = attr; } else { lastAttr.next = attr; } lastAttr = attr; } while (reader.MoveToNextAttribute()); reader.MoveToElement(); } }
// Sets the current node and current attribute parent void SetCurrentNode(CachedXmlNode node) { curNode = node; curAttrParent = (node.attributes != null) ? node: null; }
// Removes all bookmarks. All cached XML nodes will be released for garbage collection. public void RemoveAllBookmarks() { bookmarks.Clear(); cachedNodes = null; }
internal CachedXmlNode(XmlReader reader, NamespaceDecl namespacesInScope, CachedXmlNode next) : this(reader.NodeType, reader.Name, reader.LocalName, reader.Prefix, reader.NamespaceURI, reader.Value, reader.Depth, namespacesInScope, next) { }
public override void Close() { reader.Close(); curNode = null; curAttrParent = null; }