private void WritePossiblyTopLevelNode(XmlNode n, bool possiblyAttribute) { CheckState(); if (!possiblyAttribute && attribute != null) { throw new InvalidOperationException(String.Format("Current state is not acceptable for {0}.", n.NodeType)); } if (state != XmlNodeType.Element) { Document.AppendChild(n); } else if (attribute != null) { attribute.AppendChild(n); } else { current.AppendChild(n); } if (state == XmlNodeType.None) { state = XmlNodeType.XmlDeclaration; } }
// Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this. internal void ReadAttributeNodeValue(XmlReader reader, XmlAttribute attribute) { while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { attribute.AppendChild(CreateEntityReference(reader.Name), false); // omit node type check } else { // Children of Attribute is restricted to CharacterData and EntityReference (Comment is not allowed). attribute.AppendChild(CreateTextNode(reader.Value), false); // omit node type check } } }
public override XmlNode CloneNode (bool deep) { XmlNode node = new XmlAttribute (name.Prefix, name.LocalName, name.NS, OwnerDocument, true, false); if (deep) { for (XmlNode n = FirstChild; n != null; n = n.NextSibling) node.AppendChild (n.CloneNode (deep), false); } return node; }
private void ValidateAttributes(XmlElement elementNode) { XmlAttributeCollection attributes = elementNode.Attributes; XmlAttribute attr = null; for (int i = 0; i < attributes.Count; i++) { attr = attributes[i]; _currentNode = attr; //For nodeValueGetter to pick up the right attribute value if (Ref.Equal(attr.NamespaceURI, _nsXmlNs)) { //Do not validate namespace decls continue; } _validator.ValidateAttribute(attr.LocalName, attr.NamespaceURI, _nodeValueGetter, _attributeSchemaInfo); if (_psviAugmentation) { attr.XmlName = _document.AddAttrXmlName(attr.Prefix, attr.LocalName, attr.NamespaceURI, _attributeSchemaInfo); } } if (_psviAugmentation) { //Add default attributes to the attributes collection if (_defaultAttributes == null) { _defaultAttributes = new ArrayList(); } else { _defaultAttributes.Clear(); } _validator.GetUnspecifiedDefaultAttributes(_defaultAttributes); XmlSchemaAttribute schemaAttribute = null; XmlQualifiedName attrQName; attr = null; for (int i = 0; i < _defaultAttributes.Count; i++) { schemaAttribute = _defaultAttributes[i] as XmlSchemaAttribute; attrQName = schemaAttribute.QualifiedName; Debug.Assert(schemaAttribute != null); attr = _document.CreateDefaultAttribute(GetDefaultPrefix(attrQName.Namespace), attrQName.Name, attrQName.Namespace); SetDefaultAttributeSchemaInfo(schemaAttribute); attr.XmlName = _document.AddAttrXmlName(attr.Prefix, attr.LocalName, attr.NamespaceURI, _attributeSchemaInfo); attr.AppendChild(_document.CreateTextNode(schemaAttribute.AttDef.DefaultValueRaw)); attributes.Append(attr); XmlUnspecifiedAttribute defAttr = attr as XmlUnspecifiedAttribute; if (defAttr != null) { defAttr.SetSpecified(false); } } } }
private void ValidateAttributes(XmlElement elementNode) { XmlAttributeCollection attributes = elementNode.Attributes; XmlAttribute node = null; for (int i = 0; i < attributes.Count; i++) { node = attributes[i]; this.currentNode = node; if (!Ref.Equal(node.NamespaceURI, this.NsXmlNs)) { this.validator.ValidateAttribute(node.LocalName, node.NamespaceURI, this.nodeValueGetter, this.attributeSchemaInfo); if (this.psviAugmentation) { node.XmlName = this.document.AddAttrXmlName(node.Prefix, node.LocalName, node.NamespaceURI, this.attributeSchemaInfo); } } } if (this.psviAugmentation) { if (this.defaultAttributes == null) { this.defaultAttributes = new ArrayList(); } else { this.defaultAttributes.Clear(); } this.validator.GetUnspecifiedDefaultAttributes(this.defaultAttributes); XmlSchemaAttribute schemaAttribute = null; node = null; for (int j = 0; j < this.defaultAttributes.Count; j++) { schemaAttribute = this.defaultAttributes[j] as XmlSchemaAttribute; XmlQualifiedName qualifiedName = schemaAttribute.QualifiedName; node = this.document.CreateDefaultAttribute(this.GetDefaultPrefix(qualifiedName.Namespace), qualifiedName.Name, qualifiedName.Namespace); this.SetDefaultAttributeSchemaInfo(schemaAttribute); node.XmlName = this.document.AddAttrXmlName(node.Prefix, node.LocalName, node.NamespaceURI, this.attributeSchemaInfo); node.AppendChild(this.document.CreateTextNode(schemaAttribute.AttDef.DefaultValueRaw)); attributes.Append(node); XmlUnspecifiedAttribute attribute3 = node as XmlUnspecifiedAttribute; if (attribute3 != null) { attribute3.SetSpecified(false); } } } }
// Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this. internal void ReadAttributeNodeValue (XmlReader reader, XmlAttribute attribute) { while (reader.ReadAttributeValue ()) { if (reader.NodeType == XmlNodeType.EntityReference) attribute.AppendChild (CreateEntityReference (reader.Name), false); // omit node type check else // Children of Attribute is restricted to CharacterData and EntityReference (Comment is not allowed). attribute.AppendChild (CreateTextNode (reader.Value), false); // omit node type check } }
public virtual XmlNode ImportNode(XmlNode node, bool deep) { if (node == null) { throw new NullReferenceException("Null node cannot be imported."); } switch (node.NodeType) { case XmlNodeType.Attribute: XmlAttribute srcAtt = node as XmlAttribute; XmlAttribute dstAtt = this.CreateAttribute(srcAtt.Prefix, srcAtt.LocalName, srcAtt.NamespaceURI); for (XmlNode n = srcAtt.FirstChild; n != null; n = n.NextSibling) { dstAtt.AppendChild(this.ImportNode(n, deep)); } return(dstAtt); case XmlNodeType.CDATA: return(this.CreateCDataSection(node.Value)); case XmlNodeType.Comment: return(this.CreateComment(node.Value)); case XmlNodeType.Document: throw new XmlException("Document cannot be imported."); case XmlNodeType.DocumentFragment: XmlDocumentFragment df = this.CreateDocumentFragment(); if (deep) { for (XmlNode n = node.FirstChild; n != null; n = n.NextSibling) { df.AppendChild(this.ImportNode(n, deep)); } } return(df); case XmlNodeType.DocumentType: throw new XmlException("DocumentType cannot be imported."); case XmlNodeType.Element: XmlElement src = (XmlElement)node; XmlElement dst = this.CreateElement(src.Prefix, src.LocalName, src.NamespaceURI); for (int i = 0; i < src.Attributes.Count; i++) { XmlAttribute attr = src.Attributes [i]; if (attr.Specified) // copies only specified attributes { dst.SetAttributeNode((XmlAttribute)this.ImportNode(attr, deep)); } } if (deep) { for (XmlNode n = src.FirstChild; n != null; n = n.NextSibling) { dst.AppendChild(this.ImportNode(n, deep)); } } return(dst); case XmlNodeType.EndElement: throw new XmlException("Illegal ImportNode call for NodeType.EndElement"); case XmlNodeType.EndEntity: throw new XmlException("Illegal ImportNode call for NodeType.EndEntity"); case XmlNodeType.EntityReference: return(this.CreateEntityReference(node.Name)); case XmlNodeType.None: throw new XmlException("Illegal ImportNode call for NodeType.None"); case XmlNodeType.ProcessingInstruction: XmlProcessingInstruction pi = node as XmlProcessingInstruction; return(this.CreateProcessingInstruction(pi.Target, pi.Data)); case XmlNodeType.SignificantWhitespace: return(this.CreateSignificantWhitespace(node.Value)); case XmlNodeType.Text: return(this.CreateTextNode(node.Value)); case XmlNodeType.Whitespace: return(this.CreateWhitespace(node.Value)); case XmlNodeType.XmlDeclaration: XmlDeclaration srcDecl = node as XmlDeclaration; return(this.CreateXmlDeclaration(srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone)); default: throw new InvalidOperationException("Cannot import specified node type: " + node.NodeType); } }