Share via


Writing Attributes

The WriteAttributeString, WriteStartAttribute, and WriteAttributes methods are designed specifically for attribute creation. With these methods, you can write attributes on element nodes. The write attribute methods can also be used to create namespace declarations on an element. For more information, see Namespace Handling in the XmlWriter.

WriteAttributeString

The WriteAttributeString method is the simplest way to write an attribute. It is used to write an entire attribute node, including a string value. The following code adds an attribute (supplierID) and value(A23-1) to an XML element (Product):

Dim settings As New XmlWriterSettings()
settings.Indent = True
Using writer As XmlWriter = XmlWriter.Create(Console.Out, settings)
    writer.WriteStartElement("Product")
        writer.WriteAttributeString("supplierID", "A23-1")
        writer.WriteElementString("ProductID", "12345")
        writer.WriteEndElement()
End Using
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{
    writer.WriteStartElement("Product");
    writer.WriteAttributeString("supplierID", "A23-1");
    writer.WriteElementString("ProductID", "12345");
    writer.WriteEndElement();
}

The preceding code writes the following to the console:

<Product supplierID="A23-1">
  <ProductID>12345</ProductID>
</Product>

WriteStartAttribute

The WriteStartAttribute method is a more advanced version of the WriteAttributeString method. It allows you to write the attribute value using multiple method calls. For example, you can use WriteValue to write a typed value.

The attribute is closed by calling the WriteEndAttribute method.

In the following code, hireDate is a DateTime object that holds an employee hiring date. The code writes a review-date attribute, which contains the calculated value of the employee 6-month review date.

Dim hireDate As New DateTime(2008, 5, 20)
Dim settings As New XmlWriterSettings()
settings.Indent = True
Using writer As XmlWriter = XmlWriter.Create(Console.Out, settings)
    writer.WriteStartElement("Employee")
    writer.WriteStartAttribute("review-date")
    writer.WriteValue(hireDate.AddMonths(6))
    writer.WriteEndAttribute()
    writer.WriteElementString("EmployeeID", "12345")
    writer.WriteEndElement()
End Using
DateTime hireDate = new DateTime(2008, 5, 20);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{
    writer.WriteStartElement("Employee");
    writer.WriteStartAttribute("review-date");
    writer.WriteValue(hireDate.AddMonths(6));
    writer.WriteEndAttribute();
    writer.WriteElementString("EmployeeID", "12345");
    writer.WriteEndElement();
}

The preceding code writes the following to the console:

<Employee review-date="2008-11-20T00:00:00">
  <EmployeeID>12345</EmployeeID>
</Employee>

WriteAttributes

The WriteAttributes method allows you to copy all the attributes found at the current position of the supplied XmlReader object. The WriteAttributes behavior depends on the type of node on which the reader is currently positioned.

The following table describes the results of calling WriteAttributes for each node type. If the reader is positioned on a node type that is not listed in the table below, WriteAttributes has no operation.

Node type

WriteAttributes behavior

Attribute

Writes the current attribute, then the rest of the attributes until the element closing tag.

Element

Writes all attributes contained by the element.

XML Declaration

Writes all the attributes in the declaration.

For example, in the following code, the writer copies all attributes found at the current position of the reader to the writer.

Dim reader As XmlReader = XmlReader.Create("book.xml")
reader.ReadToDescendant("book")
Dim settings As New XmlWriterSettings()
settings.Indent = True
Using writer As XmlWriter = XmlWriter.Create(Console.Out, settings)
    writer.WriteStartElement("root")
    writer.WriteAttributes(reader, True)
    writer.WriteEndElement()
End Using
XmlReader reader = XmlReader.Create("book.xml");
reader.ReadToDescendant("book");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
{

    writer.WriteStartElement("root");
    writer.WriteAttributes(reader, true);
    writer.WriteEndElement();

}

The code uses this example XML file (book.xml):

<?xml version="1.0" ?>
<book genre="autobiography" 
      publicationdate="1981" 
      ISBN="1-861003-11-0">Book Title</book>

The preceding code writes the following to the console:

<root genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0" />

See Also

Other Resources

Writing XML with the XmlWriter