.NET and XML
.NET and XML Contents
Summary Is it possible to add attributes with XmlTextWriter?
In the previous example of XmlTextWriter you have seen how you can create a simple xml file. However the elements in the xml file doesn't contain any attributes like we are used from other examples in this library. In this short example we will see whether you can add attributes to the XML file, so that the XML file looks like in the previous examples.
The key to add the attributes is to use the function WriteAttributeString(). This function takes two parameters: The first one is the attribute name and the second one is the attribute value. However you can not call this function after WriteElementString. The XmlTextWriter class doesn't allow that. If you still do this, you will get the following error message:
So the answer to the question is yes, you can add attributes to the XML document, but the XmlTextWriter class won't create the XML file which we need. In another example I will show you how you can add the attribute to the MainCategory element. The XmlTextWriter will only let you add the attribute after calling WriteStartElement(). I have extended the previous example to add the attrubite. The code self contains comments which will explain you the steps to create the XML file.
Partners
Dream.In.Code dotNet Slackers dotNet Spider Your HTML Source VisualBuilder.com DevGuru Planet Source Code ZVON.ORG Web Design ASPAlliance XML Pitstop Scripts
The Spot 4 SAP Bitshop Web Hosting