XmlWriter writer = XmlWriter.Create("output.xml"); writer.WriteStartElement("person"); writer.WriteStartAttribute("name"); writer.WriteString("John Doe"); writer.WriteEndAttribute(); writer.WriteEndElement(); writer.Close();
XmlWriter writer = XmlWriter.Create("output.xml"); writer.WriteStartElement("item"); writer.WriteStartAttribute("name"); writer.WriteString("Widget"); writer.WriteEndAttribute(); writer.WriteStartAttribute("price"); writer.WriteString("1.99"); writer.WriteEndAttribute(); writer.WriteEndElement(); writer.Close();This code creates an XML document where the item element has two attributes: name and price. In both examples, the WriteStartAttribute method is called to start writing an attribute to the current element. The System.Xml.Serialization library is required to use WriteStartAttribute.