Show navigation | Frontpage | Contact us | Sitemap

Writing XML

An instance of the System.Xml.XmlWriter class is passed to the WriteXml function as an argument named xmlTarget.

public void WriteXml(Dictionary<string, string> parameters, XmlWriter xmlTarget)
{ .. }

You can use the functions exposed on the XmlWriter class or use other System.Xml classes like XmlDocument to build you XML.

Using System.Xml.XmlDocument

The System.Xml.XmlDocument class contains a WriteTo() function that write the XML of the XmlDocument to an XmlWriter.

The following code snippet will load a XML document from the local file system and save it to the XmlWriter xmlTarget:

XmlDocument doc = new XmlDocument();      
doc.Load(HttpContext.Current.Server.MapPath(
"/App_Data/SomeFile.xml"));
doc.WriteTo(xmlTarget);

The code leading up to the WriteTo() call is of course entirely up to you.

Using the XmlWriter functions

If you use the XmlWriter class to incrementally build the XML document please note the following:

  • You should call xmlTarget.WriteStartDocument() in the beginning and xmlTarget.WriteEndDocument() at the end.
  • Your XML document should contain one single root element to be valid. You can ensure this by placing an xmlTarget.WriteStartElement("MyRoot") after the document start and a matching xmlTarget.WriteEndElement() before the document end (the element name “MyRoot” is shown as an example, it is not a requirement).

Please consult MSDN or other sources for samples and a full documentation on the XmlWriter class.

My modules


© 2008 Composite A/S, Danmark
Tlf: +45 39 15 76 00 | info@composite.net
Composite A/S er Microsoft Certified Partner
Composite CMS overholder standarden for valid CSS
Composite CMS overholder standarden for valid XHTML 1.0
Printet fra http://www.composite.net/composite-1690.htm
Findes på denne sti: Composite Developer | 3.x technologies | ASP.NET | Custom XML Providers | Writing XML