System.Xml.XmlWriter.WriteAttributes C# (CSharp) Method

WriteAttributes() public method

public WriteAttributes ( XmlReader reader, bool defattr ) : void
reader XmlReader
defattr bool
return void
        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());
            }
        }

Usage Example

Example #1
0
 public virtual void WriteXml(System.Xml.XmlWriter writer)
 {
     writer.WriteStartElement("WriteAttributes");
     using (StringReader sr = new StringReader(CommonUtilities.XmlStringForAttributes))
     {
         using (XmlReader reader = new XmlTextReader(sr))
         {
             bool found = false;
             while (reader.Read())
             {
                 if (reader.Name.Equals("attributeHolder"))
                 {
                     found = true;
                     break;
                 }
             }
             if (found == false)
             {
                 throw new Exception("could not find the attributeHolder node");
             }
             // reader.ReadElementString("attributeHolder"); //moves to the root node
             writer.WriteAttributes(reader, false);
         }
     }
     writer.WriteEndElement();
 }
All Usage Examples Of System.Xml.XmlWriter::WriteAttributes