public WriteAttributeString ( string localName, string value ) : void | ||
localName | string | |
value | string | |
return | void |
using System.Xml; public class Example { public static void Main() { // Create a new XmlWriterSettings object and set various properties XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; // Create a new file called example.xml and initialize the XmlWriter XmlWriter writer = XmlWriter.Create("example.xml", settings); // Write an opening "example" element and add an attribute called "name" with a value of "example value" writer.WriteStartElement("example"); writer.WriteAttributeString("name", "example value"); // Write the closing "example" element writer.WriteEndElement(); // Close the XmlWriter to finalize the document writer.Close(); } }In this example, we create an XmlWriterSettings object to configure the XmlWriter with various properties, such as indentation and omitting the XML declaration. We then create an XmlWriter and specify the file we want to write to. Using the WriteStartElement() method, we start writing the "example" element. We then add an attribute to this element with the WriteAttributeString() method, specifying the attribute name as "name" and the attribute value as "example value". Finally, we close the "example" element with the WriteEndElement() method and close the XmlWriter to finalize the document. The package library for this class is System.Xml.
public WriteAttributeString ( string localName, string value ) : void | ||
localName | string | |
value | string | |
return | void |