// Write the attributes at the current position of an XmlReader. public virtual void WriteAttributes(XmlReader reader, bool defattr) { // Validate the parameters. if (reader == null) { throw new ArgumentNullException("reader"); } // Determine if we are on an element/xmldecl node or // on an attribute. If we are on an element/xmldecl, // then we need to reset at the end of the output. XmlNodeType type = reader.NodeType; bool reset; if (type == XmlNodeType.Element || type == XmlNodeType.XmlDeclaration) { if (!reader.MoveToFirstAttribute()) { return; } reset = true; } else if (type != XmlNodeType.Attribute) { throw new XmlException(S._("Xml_IncorrectNode")); } else { reset = false; } // Output the attributes in order. do { if (defattr || !(reader.IsDefault)) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } }while(reader.MoveToNextAttribute()); // Move back to the element if we started on one. if (reset) { reader.MoveToElement(); } }
private void LoadAttributeValue(XmlNode parent, bool direct) { XmlReader reader = this.reader; while (reader.ReadAttributeValue()) { XmlNode node; switch (reader.NodeType) { case XmlNodeType.Text: node = direct ? new XmlText(reader.Value, this.doc) : this.doc.CreateTextNode(reader.Value); break; case XmlNodeType.EntityReference: node = direct ? new XmlEntityReference(this.reader.LocalName, this.doc) : this.doc.CreateEntityReference(this.reader.LocalName); if (reader.CanResolveEntity) { reader.ResolveEntity(); this.LoadAttributeValue(node, direct); if (node.FirstChild == null) { node.AppendChildForLoad(direct ? new XmlText(string.Empty) : this.doc.CreateTextNode(string.Empty), this.doc); } } break; case XmlNodeType.EndEntity: return; default: throw UnexpectedNodeType(reader.NodeType); } parent.AppendChildForLoad(node, this.doc); } }
public static bool CheckPerspexRoot(XmlReader reader) { try { while (!reader.IsStartElement()) { reader.Read(); } if (!reader.MoveToFirstAttribute()) return false; do { if (reader.Name == "xmlns") { reader.ReadAttributeValue(); return reader.Value.ToLower() == PerspexNamespace; } } while (reader.MoveToNextAttribute()); return false; } catch { return false; } }
private void WriteAttribute(XmlReader reader, bool defattr) { if (!defattr && reader.IsDefault) { return; } WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); #if MOONLIGHT // no ReadAttributeValue() in 2.1 profile. WriteString(reader.Value); #else while (reader.ReadAttributeValue()) { switch (reader.NodeType) { case XmlNodeType.Text: WriteString(reader.Value); break; case XmlNodeType.EntityReference: WriteEntityRef(reader.Name); break; } } #endif WriteEndAttribute(); }
public override bool ReadAttributeValue() { if (validatingReader == null) { return(false); } return(validatingReader.ReadAttributeValue()); }
private XmlAttribute LoadAttributeNode() { Debug.Assert(_reader !.NodeType == XmlNodeType.Attribute); XmlReader r = _reader; if (r.IsDefault) { return(LoadDefaultAttribute()); } XmlAttribute attr = _doc !.CreateAttribute(r.Prefix, r.LocalName, r.NamespaceURI); IXmlSchemaInfo?schemaInfo = r.SchemaInfo; if (schemaInfo != null) { attr.XmlName = _doc.AddAttrXmlName(attr.Prefix, attr.LocalName, attr.NamespaceURI, schemaInfo); } while (r.ReadAttributeValue()) { XmlNode node; switch (r.NodeType) { case XmlNodeType.Text: node = _doc.CreateTextNode(r.Value); break; case XmlNodeType.EntityReference: node = _doc.CreateEntityReference(r.LocalName); if (r.CanResolveEntity) { r.ResolveEntity(); LoadAttributeValue(node, false); // Code internally relies on the fact that an EntRef nodes has at least one child (even an empty text node). Ensure that this holds true, // if the reader does not present any children for the ent-ref if (node.FirstChild == null) { node.AppendChildForLoad(_doc.CreateTextNode(string.Empty), _doc); } } break; default: throw UnexpectedNodeType(r.NodeType); } Debug.Assert(node != null); attr.AppendChildForLoad(node, _doc); } return(attr); }
// Read the children of a node. internal void ReadChildren(XmlReader r, XmlNode parent) { if (parent is XmlAttribute) { while (r.ReadAttributeValue()) { XmlNodeType nodeType = r.NodeType; if (nodeType == XmlNodeType.Text) { parent.AppendChild(CreateTextNode(r.Value)); } else if (nodeType == XmlNodeType.EntityReference) { parent.AppendChild(CreateEntityReference(r.Name)); } else { throw new InvalidOperationException(/* TODO */); } } } else { int startDepth = r.Depth; r.Read(); while (r.ReadState == ReadState.Interactive && r.Depth != startDepth) { // read child XmlNode child = ReadNodeInternal(r); // ignore nulls, at least for now if (child != null) { // append child parent.AppendChild(child); } } // if we hit end-of-file, give an error if (r.Depth != startDepth) { throw new InvalidOperationException(/* TODO */); } // if we have an element, read past the end element node if (parent is XmlElement) { r.Read(); } } }
void IXmlSerializable.ReadXml(XmlReader reader) { reader.ReadStartElement(); while (reader.GetAttribute("key") != null) { reader.ReadAttributeValue(); this.Add(reader.GetAttribute("key"), reader.GetAttribute("value")); reader.ReadStartElement("property"); } reader.ReadEndElement(); }
// Iterates through the current attribute value's text and entity references chunks. public override bool ReadAttributeValue() { if (parsingFunction == ParsingFunction.InReadBinaryContent) { parsingFunction = ParsingFunction.Read; readBinaryHelper.Finish(); } if (!coreReader.ReadAttributeValue()) { return(false); } parsingFunction = ParsingFunction.Read; return(true); }
// 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 async Task WriteAttributesAsync(XmlReader reader, bool defattr) { if (null == reader) { throw new ArgumentNullException(nameof(reader)); } if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.XmlDeclaration) { if (reader.MoveToFirstAttribute()) { await WriteAttributesAsync(reader, defattr).ConfigureAwait(false); reader.MoveToElement(); } } else if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException(SR.Xml_InvalidPosition, string.Empty); } else { do { // we need to check both XmlReader.IsDefault and XmlReader.SchemaInfo.IsDefault. // If either of these is true and defattr=false, we should not write the attribute out if (defattr || !reader.IsDefaultInternal) { await WriteStartAttributeAsync(reader.Prefix, reader.LocalName, reader.NamespaceURI).ConfigureAwait(false); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { await WriteEntityRefAsync(reader.Name).ConfigureAwait(false); } else { await WriteStringAsync(reader.Value).ConfigureAwait(false); } } await WriteEndAttributeAsync().ConfigureAwait(false); } }while (reader.MoveToNextAttribute()); } }
// XmlReader Helper Methods // Writes out all the attributes found at the current position in the specified XmlReader. public virtual void WriteAttributes(XmlReader reader, bool defattr) { if (null == reader) { throw new ArgumentNullException("reader"); } if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.XmlDeclaration) { if (reader.MoveToFirstAttribute()) { WriteAttributes(reader, defattr); reader.MoveToElement(); } } else if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException(Res.Xml_InvalidPosition, string.Empty); } else { do { // we need to check both XmlReader.IsDefault and XmlReader.SchemaInfo.IsDefault. // If either of these is true and defattr=false, we should not write the attribute out IXmlSchemaInfo schemaInfo; if (defattr || (!reader.IsDefault && ((schemaInfo = reader.SchemaInfo) == null || !schemaInfo.IsDefault))) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } }while (reader.MoveToNextAttribute()); } }
private XmlAttribute LoadAttributeNode() { XmlReader reader = this.reader; if (reader.IsDefault) { return(this.LoadDefaultAttribute()); } XmlAttribute attribute = this.doc.CreateAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); IXmlSchemaInfo schemaInfo = reader.SchemaInfo; if (schemaInfo != null) { attribute.XmlName = this.doc.AddAttrXmlName(attribute.Prefix, attribute.LocalName, attribute.NamespaceURI, schemaInfo); } while (reader.ReadAttributeValue()) { XmlNode node; switch (reader.NodeType) { case XmlNodeType.Text: node = this.doc.CreateTextNode(reader.Value); break; case XmlNodeType.EntityReference: node = this.doc.CreateEntityReference(reader.LocalName); if (reader.CanResolveEntity) { reader.ResolveEntity(); this.LoadAttributeValue(node, false); if (node.FirstChild == null) { node.AppendChildForLoad(this.doc.CreateTextNode(string.Empty), this.doc); } } break; default: throw UnexpectedNodeType(reader.NodeType); } attribute.AppendChildForLoad(node, this.doc); } return(attribute); }
// XmlReader Helper Methods /// <include file='doc\XmlWriter.uex' path='docs/doc[@for="XmlWriter.WriteAttributes"]/*' /> /// <devdoc> /// <para>Writes out all the attributes found at the current /// position in the specified XmlReader.</para> /// </devdoc> public virtual void WriteAttributes(XmlReader reader, bool defattr) { if (null == reader) { throw new ArgumentNullException("reader"); } if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.XmlDeclaration) { if (reader.MoveToFirstAttribute()) { WriteAttributes(reader, defattr); reader.MoveToElement(); } } else if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException(Res.Xml_InvalidPosition, string.Empty); } else { do { if (defattr || !reader.IsDefault) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } }while (reader.MoveToNextAttribute()); } }
private void LoadAttributeValue(XmlNode parent, bool direct) { XmlReader r = _reader; while (r.ReadAttributeValue()) { XmlNode node; switch (r.NodeType) { case XmlNodeType.Text: node = direct ? new XmlText(r.Value, _doc) : _doc.CreateTextNode(r.Value); break; case XmlNodeType.EndEntity: return; case XmlNodeType.EntityReference: node = direct ? new XmlEntityReference(_reader.LocalName, _doc) : _doc.CreateEntityReference(_reader.LocalName); if (r.CanResolveEntity) { r.ResolveEntity(); LoadAttributeValue(node, direct); // Code internally relies on the fact that an EntRef nodes has at least one child (even an empty text node). Ensure that this holds true, // if the reader does not present any children for the ent-ref if (node.FirstChild == null) { node.AppendChildForLoad(direct ? new XmlText(string.Empty) : _doc.CreateTextNode(string.Empty), _doc); } } break; default: throw UnexpectedNodeType(r.NodeType); } Debug.Assert(node != null); parent.AppendChildForLoad(node, _doc); } return; }
public static void Run(XmlReader reader, bool includeAttributes, bool includeAttributeValues = true) { using (StreamWriter output = File.CreateText(Path.Combine(Program.TempPath, reader.GetType().Name + "-output.csv"))) { var properties = typeof(XmlReader).GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.GetIndexParameters().Length == 0 && !ignoredProperties.Contains(p.Name)) .ToArray(); output.WriteLine(CSV(properties.Select(p => p.Name))); do { output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null))))); if (includeAttributes && reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null))))); if (includeAttributeValues) { reader.ReadAttributeValue(); output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null))))); } } } } while (reader.Read()); output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null))))); } }
public void ReadAttributeValue (XmlReader reader) { reader.Read (); // root Assert.IsTrue (reader.MoveToFirstAttribute ()); // It looks like that MS.NET shows AttributeCount and // HasAttributes as the same as element node! this.AssertNodeValues ("#1", reader, XmlNodeType.Attribute, 1, false, "attr", "", "attr", "", "", true, 1, true); Assert.IsTrue (reader.ReadAttributeValue ()); // MS.NET XmlTextReader fails. Its Prefix returns // null instead of "". It is fixed in MS.NET 2.0. this.AssertNodeValues ("#2", reader, XmlNodeType.Text, 2, false, "", "", "", "", "", true, 1, true); Assert.IsTrue (reader.MoveToElement ()); this.AssertNodeValues ("#3", reader, XmlNodeType.Element, 0, true, "root", "", "root", "", "", false, 1, true); }
// XmlReader Helper Methods // Writes out all the attributes found at the current position in the specified XmlReader. public virtual void WriteAttributes(XmlReader reader, bool defattr) { if (null == reader) { throw new ArgumentNullException(nameof(reader)); } if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.XmlDeclaration) { if (reader.MoveToFirstAttribute()) { WriteAttributes(reader, defattr); reader.MoveToElement(); } } else if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException(SR.Xml_InvalidPosition, string.Empty); } else { do { // we need to check both XmlReader.IsDefault and XmlReader.SchemaInfo.IsDefault. // If either of these is true and defattr=false, we should not write the attribute out if (defattr || !reader.IsDefaultInternal) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } } while (reader.MoveToNextAttribute()); } }
public override bool ReadAttributeValue() { CheckAsync(); return(_coreReader.ReadAttributeValue()); }
public void MoveToXmlDeclAttributes (XmlReader xmlReader) { xmlReader.Read (); this.AssertNodeValues ("#1", xmlReader, XmlNodeType.XmlDeclaration, 0, false, "xml", String.Empty, "xml", String.Empty, "version=\"1.0\" standalone=\"yes\"", 2); Assert.IsTrue (xmlReader.MoveToFirstAttribute (), "MoveToFirstAttribute"); this.AssertNodeValues ("#2", xmlReader, XmlNodeType.Attribute, 0, // FIXME: might be 1 false, "version", String.Empty, "version", String.Empty, "1.0", 2); xmlReader.ReadAttributeValue (); this.AssertNodeValues ("#3", xmlReader, XmlNodeType.Text, 1, // FIXME might be 2 false, String.Empty, null, // FIXME: should be String.Empty, String.Empty, null, // FIXME: should be String.Empty, "1.0", 2); xmlReader.MoveToNextAttribute (); this.AssertNodeValues ("#4", xmlReader, XmlNodeType.Attribute, 0, // FIXME: might be 1 false, "standalone", String.Empty, "standalone", String.Empty, "yes", 2); xmlReader.ReadAttributeValue (); this.AssertNodeValues ("#5", xmlReader, XmlNodeType.Text, 1, // FIXME: might be 2 false, String.Empty, null, // FIXME: should be String.Empty, String.Empty, null, // FIXME: should be String.Empty, "yes", 2); }
/// <summary> /// This method is overriden to filter out attributes which are not allowed /// </summary> public override void WriteAttributes(XmlReader reader, bool defattr) { if (Options.FilterOutput) { // The following code is copied from implementation of XmlWriter's // WriteAttributes method. if (reader == null) { throw new ArgumentNullException("reader"); } if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration)) { if (reader.MoveToFirstAttribute()) { WriteAttributes(reader, defattr); reader.MoveToElement(); } } else { if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException("Xml_InvalidPosition"); } do { if (defattr || !reader.IsDefault) { string attributename = reader.LocalName.ToLower(); // Check if the attribute is allowed bool canWrite = true; switch (LastStartElement) { case "embed": canWrite = true; break; case "img": if (Options.RewriteImgSize.HasValue && Options.RewriteImgSize.Value.Width <= 0 && Options.RewriteImgSize.Value.Height <= 0 && (attributename == "width" || attributename == "height")) { canWrite = false; } break; default: canWrite = (Array.IndexOf(Options.AllowedAttributes, attributename) >= 0); break; } // If allowed, write the attribute if (canWrite) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); } while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { if (canWrite) { WriteEntityRef(reader.Name); } continue; } if (canWrite) { string value = reader.Value; if (Options.BaseHref != null && LastStartElement == "a" && attributename == "href") { value = HtmlUriExtractor.TryCreate( Options.BaseHref, reader.Value, value); } if (Options.BaseHref != null && (LastStartElement == "img" || LastStartElement == "embed") && attributename == "src") { value = HtmlUriExtractor.TryCreate( Options.BaseHref, reader.Value, value); } if (Options.RewriteImgSrc != null && LastStartElement == "img" && attributename == "src") { value = Options.RewriteImgSrc.ToString().Replace("{url}", Renderer.UrlEncode(value)); } else if (Options.RewriteImgSize.HasValue && LastStartElement == "img" && attributename == "width") { value = Options.RewriteImgSize.Value.Width.ToString(); } else if (Options.RewriteImgSize.HasValue && LastStartElement == "img" && attributename == "height") { value = Options.RewriteImgSize.Value.Height.ToString(); } if (LastStartElement == "link" && attributename == "rel") { switch (value.ToLower()) { case "stylesheet": value = "stylesheet-stripped"; break; } } WriteString(value); } } if (canWrite) { WriteEndAttribute(); } } } while (reader.MoveToNextAttribute()); } } else { base.WriteAttributes(reader, defattr); } }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="name"></param> protected bool MoveToAttribute(XmlReader reader, string name, bool throwIfNotFound) { if(!reader.MoveToAttribute(name)) { if (throwIfNotFound) throw new AttributeNotFoundException(name); return false; } if (!reader.ReadAttributeValue()) { if (throwIfNotFound) throw new AttributeNotFoundException(name); return false; } return true; }
private void TestAttributeDefaultContributionInternal (XmlReader dvr) { dvr.Read (); // DTD dvr.Read (); Assert.AreEqual (XmlNodeType.Element, dvr.NodeType); Assert.AreEqual ("root", dvr.Name); Assert.AreEqual (2, dvr.AttributeCount); // foo Assert.IsTrue (dvr.MoveToFirstAttribute ()); Assert.AreEqual ("foo", dvr.Name); Assert.AreEqual ("foo-def", dvr ["foo"]); Assert.IsNotNull (dvr ["bar"]); Assert.AreEqual ("foo-def", dvr.Value); Assert.IsTrue (dvr.ReadAttributeValue ()); Assert.AreEqual (XmlNodeType.Text, dvr.NodeType); Assert.AreEqual (String.Empty, dvr.Name); Assert.AreEqual ("foo-def", dvr.Value); // bar Assert.IsTrue (dvr.MoveToNextAttribute ()); Assert.AreEqual ("bar", dvr.Name); Assert.AreEqual ("foo-def", dvr ["foo"]); Assert.IsNotNull (dvr ["bar"]); Assert.AreEqual ("bar-def", dvr.Value); Assert.IsTrue (dvr.ReadAttributeValue ()); Assert.AreEqual (XmlNodeType.Text, dvr.NodeType); Assert.AreEqual (String.Empty, dvr.Name); Assert.AreEqual ("bar-def", dvr.Value); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="name"></param> protected void MoveToAttribute(XmlReader reader, string name) { if(!reader.MoveToAttribute(name)) throw new Exception("could not find attribute"); if (!reader.ReadAttributeValue()) throw new Exception("attribute empty"); }
/// <summary> /// This method is overriden to filter out attributes which are not allowed /// </summary> public override void WriteAttributes(XmlReader reader, bool defattr) { if( this.FilterOutput ) { // The following code is copied from implementation of XmlWriter's // WriteAttributes method. if (reader == null) { throw new ArgumentNullException("reader"); } if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration)) { if (reader.MoveToFirstAttribute()) { this.WriteAttributes(reader, defattr); reader.MoveToElement(); } } else { if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException("Xml_InvalidPosition"); } do { if (defattr || !reader.IsDefault) { // Check if the attribute is allowed bool canWrite = false; string attributeLocalName = reader.LocalName.ToLower(); foreach( string name in this.AllowedAttributes ) { if( name == attributeLocalName ) { canWrite = true; break; } } // If allowed, write the attribute if( canWrite ) this.WriteStartAttribute(reader.Prefix, attributeLocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { if( canWrite ) this.WriteEntityRef(reader.Name); continue; } if( canWrite )this.WriteString(reader.Value); } if( canWrite ) this.WriteEndAttribute(); } } while (reader.MoveToNextAttribute()); } } else { base.WriteAttributes( reader, defattr ); } }
public override void WriteAttributes(XmlReader reader, bool defattr) { if (reader == null) { throw new ArgumentNullException("reader"); } if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration)) { if (reader.MoveToFirstAttribute()) { this.WriteAttributes(reader, defattr); reader.MoveToElement(); } } else { if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException("Xml_InvalidPosition"); } do { if (defattr || !reader.IsDefault) { string attributename = reader.LocalName.ToLower(); this.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { this.WriteEntityRef(reader.Name); continue; } string value = reader.Value; if (BaseHref != null && LastStartElement == "a" && attributename == "href") { value = HtmlUriExtractor.TryCreate( BaseHref, reader.Value, value); } if (BaseHref != null && (LastStartElement == "img" || LastStartElement == "embed") && attributename == "src") { value = HtmlUriExtractor.TryCreate( BaseHref, reader.Value, value); } this.WriteString(value); } this.WriteEndAttribute(); } } while (reader.MoveToNextAttribute()); } }
protected void ConsumeReader(XmlReader reader) { while (reader.Read()) { string x = reader.Name + reader.NodeType + reader.Value; if (reader.NodeType == XmlNodeType.Element) { if (reader.HasAttributes) { reader.MoveToFirstAttribute(); int index = 0; reader.MoveToAttribute(index); index++; while (reader.MoveToNextAttribute()) { string name = reader.Name; string value; value = reader.GetAttribute(index); value = reader.GetAttribute(name); value = reader.GetAttribute(name, null); reader.ReadAttributeValue(); reader.MoveToAttribute(index); reader.MoveToAttribute(name, null); index++; } } } if (reader.NodeType == XmlNodeType.EndElement) { reader.Skip(); } } }
// Write the contents of a node from an XmlReader. public virtual void WriteNode(XmlReader reader, bool defattr) { // Validate the parameters. if (reader == null) { throw new ArgumentNullException("reader"); } // Loop until we have finished with the current node. uint level = 0; do { switch (reader.NodeType) { case XmlNodeType.Attribute: { // Write this attribute only - we only get here // if we started on an attribute. if (defattr || !(reader.IsDefault)) { WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } reader.MoveToNextAttribute(); return; } // Not reached. case XmlNodeType.CDATA: { WriteCData(reader.Value); } break; case XmlNodeType.Comment: { WriteComment(reader.Value); } break; case XmlNodeType.DocumentType: { WriteDocType (reader.Name, reader["PUBLIC"], reader["SYSTEM"], reader.Value); } break; case XmlNodeType.Element: { // Write the starting information for the element. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI); // Write all of the element attributes. WriteAttributes(reader, defattr); // End the element, or go in a level. if (reader.IsEmptyElement) { WriteEndElement(); } else { ++level; } } break; case XmlNodeType.EndElement: { WriteFullEndElement(); --level; } break; case XmlNodeType.EntityReference: { WriteEntityRef(reader.Name); } break; case XmlNodeType.None: { // We are probably at EOF, so bail out. return; } // Not reached. case XmlNodeType.ProcessingInstruction: case XmlNodeType.XmlDeclaration: { WriteProcessingInstruction (reader.Name, reader.Value); } break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: { WriteWhitespace(reader.Value); } break; case XmlNodeType.Text: { WriteString(reader.Value); } break; default: break; } }while(reader.Read() && level > 0); }
private void WriteAttribute (XmlReader reader, bool defattr) { if (!defattr && reader.IsDefault) return; WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI); #if MOONLIGHT // no ReadAttributeValue() in 2.1 profile. WriteString (reader.Value); #else while (reader.ReadAttributeValue ()) { switch (reader.NodeType) { case XmlNodeType.Text: WriteString (reader.Value); break; case XmlNodeType.EntityReference: WriteEntityRef (reader.Name); break; } } #endif WriteEndAttribute (); }
// Read the children of a node. internal void ReadChildren(XmlReader r, XmlNode parent) { if(parent is XmlAttribute) { while(r.ReadAttributeValue()) { XmlNodeType nodeType = r.NodeType; if(nodeType == XmlNodeType.Text) { parent.AppendChild(CreateTextNode(r.Value)); } else if(nodeType == XmlNodeType.EntityReference) { parent.AppendChild(CreateEntityReference(r.Name)); } else { throw new InvalidOperationException(/* TODO */); } } } else { int startDepth = r.Depth; r.Read(); while(r.ReadState == ReadState.Interactive && r.Depth != startDepth) { // read child XmlNode child = ReadNodeInternal(r); // ignore nulls, at least for now if(child != null) { // append child parent.AppendChild(child); } } // if we hit end-of-file, give an error if(r.Depth != startDepth) { throw new InvalidOperationException(/* TODO */); } // if we have an element, read past the end element node if(parent is XmlElement) { r.Read(); } } }
// Write the contents of a node from an XmlReader. public virtual void WriteNode(XmlReader reader, bool defattr) { // Validate the parameters. if(reader == null) { throw new ArgumentNullException("reader"); } // Loop until we have finished with the current node. uint level = 0; do { switch(reader.NodeType) { case XmlNodeType.Attribute: { // Write this attribute only - we only get here // if we started on an attribute. if(defattr || !(reader.IsDefault)) { WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI); while(reader.ReadAttributeValue()) { if(reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } reader.MoveToNextAttribute(); return; } // Not reached. case XmlNodeType.CDATA: { WriteCData(reader.Value); } break; case XmlNodeType.Comment: { WriteComment(reader.Value); } break; case XmlNodeType.DocumentType: { WriteDocType (reader.Name, reader["PUBLIC"], reader["SYSTEM"], reader.Value); } break; case XmlNodeType.Element: { // Write the starting information for the element. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI); // Write all of the element attributes. WriteAttributes(reader, defattr); // End the element, or go in a level. if(reader.IsEmptyElement) { WriteEndElement(); } else { ++level; } } break; case XmlNodeType.EndElement: { WriteFullEndElement(); --level; } break; case XmlNodeType.EntityReference: { WriteEntityRef(reader.Name); } break; case XmlNodeType.None: { // We are probably at EOF, so bail out. return; } // Not reached. case XmlNodeType.ProcessingInstruction: case XmlNodeType.XmlDeclaration: { WriteProcessingInstruction (reader.Name, reader.Value); } break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: { WriteWhitespace(reader.Value); } break; case XmlNodeType.Text: { WriteString(reader.Value); } break; default: break; } } while(reader.Read() && level > 0); }
// Write the attributes at the current position of an XmlReader. public virtual void WriteAttributes(XmlReader reader, bool defattr) { // Validate the parameters. if(reader == null) { throw new ArgumentNullException("reader"); } // Determine if we are on an element/xmldecl node or // on an attribute. If we are on an element/xmldecl, // then we need to reset at the end of the output. XmlNodeType type = reader.NodeType; bool reset; if(type == XmlNodeType.Element || type == XmlNodeType.XmlDeclaration) { if(!reader.MoveToFirstAttribute()) { return; } reset = true; } else if(type != XmlNodeType.Attribute) { throw new XmlException(S._("Xml_IncorrectNode")); } else { reset = false; } // Output the attributes in order. do { if(defattr || !(reader.IsDefault)) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while(reader.ReadAttributeValue()) { if(reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } } while(reader.MoveToNextAttribute()); // Move back to the element if we started on one. if(reset) { reader.MoveToElement(); } }
static Figure FromXml(XmlReader re) { Figure result = null; re.MoveToContent(); re.MoveToAttribute(TYPE_ATTR); while (re.ReadAttributeValue()) { if (re.NodeType == XmlNodeType.Text) { Type figureType = Type.GetType(re.Value); if (figureType != null) result = (Figure)Activator.CreateInstance(figureType); } } // apply properties to figure if (result != null) { while (re.Read()) { if (re.NodeType == XmlNodeType.Element) { if (re.LocalName == USERATTRS_ELE) ApplyUserAttrs(re.ReadSubtree(), result); else if (re.LocalName == DIMENSION_ELE && result is IDimension) ApplyDimensions(re.ReadSubtree(), (IDimension)result); else if (re.LocalName == FILL_ELE && result is IFillable) ApplyFill(re.ReadSubtree(), (IFillable)result); else if (re.LocalName == STROKE_ELE && result is IStrokeable) ApplyStroke(re.ReadSubtree(), (IStrokeable)result); else if (re.LocalName == ALPHA_ELE && result is IAlphaBlendable) ApplyAlpha(re.ReadSubtree(), (IAlphaBlendable)result); else if (re.LocalName == IMAGE_ELE && result is IImage) ApplyImage(re.ReadSubtree(), (IImage)result); else if (re.LocalName == BITMAP_ELE && result is IBitmap) ApplyBitmap(re.ReadSubtree(), (IBitmap)result); else if (re.LocalName == METAFILE_ELE && result is IMetafile) ApplyMetafile(re.ReadSubtree(), (IMetafile)result); else if (re.LocalName == TEXT_ELE && result is ITextable) ApplyText(re.ReadSubtree(), (ITextable)result); else if (re.LocalName == CHILDFIGURES_ELE && result is IChildFigureable) ApplyChildren(re.ReadSubtree(), (IChildFigureable)result); else if (re.LocalName == POLYLINE_ELE && result is IPolyline) ApplyPolyline(re.ReadSubtree(), (IPolyline)result); else if (re.LocalName == MARKER_ELE && result is IMarkable) ApplyMarkers(re.ReadSubtree(), (IMarkable)result); else if (re.LocalName == EDITABLEATTRIBUTES_ELE && result is IEditable) ApplyEditableAttributes(re.ReadSubtree(), (IEditable)result); else if (re.LocalName == POLYGON_ELE && result is PolygonFigure) ApplyPolygon(re.ReadSubtree(), (PolygonFigure)result); } } } return result; }
// 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 } }
/// <summary> /// This method is overriden to filter out attributes which are not allowed /// </summary> public override void WriteAttributes(XmlReader reader, bool defattr) { // The following code is copied from implementation of XmlWriter's // WriteAttributes method. if (reader == null) { throw new ArgumentNullException("reader"); } if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration)) { if (reader.MoveToFirstAttribute()) { try { this.WriteAttributes(reader, defattr); } catch { } reader.MoveToElement(); } } else { if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException("Xml_InvalidPosition"); } do { try { if (defattr || !reader.IsDefault) { // Check if the attribute is allowed string attributeLocalName = reader.LocalName.ToLower(); AttributeTypes type = GetAttributeType(attributeLocalName); if (!type.Equals(AttributeTypes.Remove)) { //parse the attribute string attribteValue = ""; while (reader.ReadAttributeValue()) { if (reader.NodeType.Equals(XmlNodeType.Text)) attribteValue += reader.Value; } string parsedAttribteValue = ParseAttribute(attribteValue, type); //write the if (parsedAttribteValue.Length > 0) { this.WriteStartAttribute(attributeLocalName); this.WriteRaw(parsedAttribteValue); //this.WriteString(parsedAttribteValue); this.WriteEndAttribute(); } } } } catch { } } while (reader.MoveToNextAttribute()); } }
public override bool ReadAttributeValue() { return(reader.ReadAttributeValue()); }
public virtual void WriteAttributes(XmlReader reader, bool defattr) { if (reader == null) { throw new ArgumentNullException("reader"); } if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration)) { if (reader.MoveToFirstAttribute()) { this.WriteAttributes(reader, defattr); reader.MoveToElement(); } } else { if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException("Xml_InvalidPosition", string.Empty); } do { if (defattr || !reader.IsDefaultInternal) { this.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { this.WriteEntityRef(reader.Name); } else { this.WriteString(reader.Value); } } this.WriteEndAttribute(); } } while (reader.MoveToNextAttribute()); } }
static void ReadEndpointCheck(XmlReader reader) { reader.MoveToAttribute ("name"); if (!reader.ReadAttributeValue ()) throw new ArgumentException ("Cannot read endpoint"); }
// XmlReader Helper Methods /// <include file='doc\XmlWriter.uex' path='docs/doc[@for="XmlWriter.WriteAttributes"]/*' /> /// <devdoc> /// <para>Writes out all the attributes found at the current /// position in the specified XmlReader.</para> /// </devdoc> public virtual void WriteAttributes(XmlReader reader, bool defattr) { if (null==reader) { throw new ArgumentNullException("reader"); } if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.XmlDeclaration) { if (reader.MoveToFirstAttribute()) { WriteAttributes(reader, defattr); reader.MoveToElement(); } } else if (reader.NodeType != XmlNodeType.Attribute) { throw new XmlException(Res.Xml_InvalidPosition); } else { do { if (defattr || ! reader.IsDefault) { WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { WriteEntityRef(reader.Name); } else { WriteString(reader.Value); } } WriteEndAttribute(); } } while (reader.MoveToNextAttribute()); } }
internal int AppendAttributeValue(XmlReader reader) { int charactersWritten = 0; char quote = '"';//reader.QuoteChar; // // In !DOCTYPE, quote is '\0' for second public attribute. // Protect ourselves from writing invalid XML by always // supplying a valid quote char. // if (quote != '"' && quote != '\'') { quote = '"'; } charactersWritten += Write(quote); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.Text) { charactersWritten += AppendEscapeXmlString(reader.Value, true, quote); } else { charactersWritten += AppendEntityRef(reader.Name); } } charactersWritten += Write(quote); return charactersWritten; }
private void VerifyAttributeValue(XmlReader expectedReader, XmlReader actualReader, bool useReadAttributeValue) { this.VerifyReaderNodesAreEqual(expectedReader, actualReader); if (expectedReader.NodeType != XmlNodeType.Attribute || !useReadAttributeValue) { return; } while (expectedReader.ReadAttributeValue()) { this.Assert.AreEqual(true, actualReader.ReadAttributeValue(), "ReadAttributeValue differs."); this.VerifyReaderNodesAreEqual(expectedReader, actualReader); } this.Assert.AreEqual(false, actualReader.ReadAttributeValue(), "ReadAttributeValue differs."); this.VerifyReaderNodesAreEqual(expectedReader, actualReader); }