// // Summary: // When overridden in a derived class, writes the specified start tag and associates // it with the given namespace and prefix. // // Parameters: // localName: // The local name of the element. // // prefix: // The namespace prefix of the element. // // ns: // The namespace URI to associate with the element. // // Exceptions: // System.InvalidOperationException: // The writer is closed. public string WriteStartElement(string prefix, string localName, string ns) { if (_WriteState == WriteState.Attribute) throw new InvalidOperationException("writer is closed"); if (prefix == "xmlns") throw new ArgumentException("Prefix \"xmlns\" is reserved for use by XML."); if (localName == null || localName.Length == 0) throw new ArgumentException("The empty string '' is not a valid local name."); // Check to see if a started attribute need finished if (_WriteState == WriteState.Attribute) this.WriteEndAttribute(); string localPrefix = ""; string localNS = ""; string childPrefix = ""; if(prefix != null && prefix.Length == 0) prefix = null; // if prefix == null and ns == null, only localName will write // if prefix == null and ns != null, prefix is set to xmlns and ns will be writen // if prefix != null && ns == null, use prefix if (prefix != null && ns != null) { localPrefix = prefix + ":"; localNS = " xmlns:" + prefix + "=" + "\"" + ns + "\""; childPrefix = prefix; } else if (prefix != null && ns == null) localPrefix = prefix + ":"; else if (prefix == null && ns != null) { if (_ElementStack.Count > 0) { if(((ElementInfo)_ElementStack.Peek()).NameSpaceName != ns) { childPrefix = "b" + _autoPrefixIndex++; localNS = " xmlns:" + childPrefix + "=" + "\"" + ns + "\""; } else { prefix = ((ElementInfo)_ElementStack.Peek()).ChildNSPrefix; if(prefix != null && prefix != "") { localPrefix = prefix + ":"; childPrefix = prefix; } } } else { localNS = " xmlns" + "=" + "\"" + ns + "\""; } } else if(_ElementStack.Count > 0) { prefix = ((ElementInfo)_ElementStack.Peek()).ChildNSPrefix; if(prefix != null && prefix != "") { localPrefix = prefix + ":"; } else { prefix = null; } } // Check to see if we need to close a previous start tag if (_ElementStack.Count > 0) if (((ElementInfo)_ElementStack.Peek()).State == WriteState.Element) this.WriteRaw(">"); // Write the element this.WriteRaw("<" + localPrefix + localName + localNS); // Create a new Element and Push the element onto the ElementStack ElementInfo currentElement = new ElementInfo(); currentElement.Name = localName; currentElement.NSPrefix = prefix; currentElement.ChildNSPrefix = childPrefix; currentElement.NameSpaceName = ns; currentElement.IsEmpty = true; // Anytime a new Element is added the previous must have its last known state and IsEmpty // state updated to reflect we are adding Content and the element is no longer Empty if (_ElementStack.Count > 0) { ElementInfo ei = (ElementInfo)_ElementStack.Pop(); ei.State = _WriteState = WriteState.Content; // Signal that this element has content ei.IsEmpty = false; _ElementStack.Push(ei); } // Update the local WriteState and new Element State currentElement.State = _WriteState = WriteState.Element; // Push the new element onto the stack _ElementStack.Push(currentElement); return prefix; }
public void Push(ElementInfo element) { _ElementList.Insert(0, (ElementInfo)element); }
public ElementInfo Pop() { if (_ElementList.Count == 0) throw new ArgumentException("Token EndElement in state EndRootElement would result in an invalid XML document."); ElementInfo tempElement = new ElementInfo(); tempElement = (ElementInfo)_ElementList[0]; _ElementList.RemoveAt(0); return tempElement; }
// // Summary: // When overridden in a derived class, writes the specified start tag and associates // it with the given namespace and prefix. // // Parameters: // localName: // The local name of the element. // // prefix: // The namespace prefix of the element. // // ns: // The namespace URI to associate with the element. // // Exceptions: // System.InvalidOperationException: // The writer is closed. public void WriteStartElement(string prefix, string localName, string ns) { if (_WriteState == WriteState.Attribute) { throw new InvalidOperationException("writer is closed"); } if (prefix == "xmlns") { throw new ArgumentException("Prefix \"xmlns\" is reserved for use by XML."); } if (localName == null || localName.Length == 0) { throw new ArgumentException("The empty string '' is not a valid local name."); } // Check to see if a started attribute need finished if (_WriteState == WriteState.Attribute) { this.WriteEndAttribute(); } string localPrefix = ""; string localNS = ""; string childPrefix = ""; // if prefix == null and ns == null, only localName will write // if prefix == null and ns != null, prefix is set to xmlns and ns will be writen // if prefix != null && ns == null, use prefix if (prefix != null && ns != null) { localPrefix = prefix + ":"; localNS = " xmlns:" + prefix + "=" + "\"" + ns + "\""; childPrefix = prefix; } else if (prefix != null && ns == null) { localPrefix = prefix + ":"; } else if (prefix == null && ns != null) { if (_ElementStack.Count > 0) { if (((ElementInfo)_ElementStack[0]).NameSpaceName != ns) { childPrefix = "b" + _autoPrefixIndex++; localNS = " xmlns:" + childPrefix + "=" + "\"" + ns + "\""; } else { prefix = ((ElementInfo)_ElementStack[0]).ChildNSPrefix; if (prefix != null && prefix != "") { localPrefix = prefix + ":"; } } } else { localNS = " xmlns" + "=" + "\"" + ns + "\""; } } else if (_ElementStack.Count > 0) { prefix = ((ElementInfo)_ElementStack[0]).ChildNSPrefix; if (prefix != null && prefix != "") { localPrefix = prefix + ":"; } } // Check to see if we need to close a previous start tag if (_ElementStack.Count > 0) { if (_ElementStack[0].State == WriteState.Element) { this.WriteRaw(">"); } } // Write the element this.WriteRaw("<" + localPrefix + localName + localNS); // Create a new Element and Push the element onto the ElementStack ElementInfo currentElement = new ElementInfo(); currentElement.Name = localName; currentElement.NSPrefix = prefix; currentElement.ChildNSPrefix = childPrefix; currentElement.NameSpaceName = ns; currentElement.IsEmpty = true; // Anytime a new Element is added the previous must have its last known state and IsEmpty // state updated to reflect we are adding Content and the element is no longer Empty if (_ElementStack.Count > 0) { _ElementStack[0].IsEmpty = false; _ElementStack[0].State = WriteState.Content; } // Update the local WriteState and new Element State currentElement.State = _WriteState = WriteState.Element; // Push the new element onto the stack _ElementStack.Push(currentElement); }
public void Push(ElementInfo element) { _ElementList.Insert(0, (ElementInfo)element); }