13.6 Programmatically Generating XML Documents

 <  Day Day Up  >  

You want to programmatically add or remove elements and attributes from an XML document.


Technique

The XmlDocument class contains several methods that allow you to generate various nodes within an XML document. The first step is to find the node that you will append the new node to. For instance, if you create a new attribute using the CreateAttribute method, you need a corresponding element node to append it to. For new elements, you need to know the node that you will use as the axes for node insertion depends on which insertion method you call.

To create new attribute for an existing element, call the CreateAttribute method, passing a string for the attribute name . It returns an XmlAttribute object whose Value property is used for the attribute value. To add the new attribute to an existing element, call the SetNamedItem method defined in the Attributes collection of the selected XmlNode object:

 
 // get selected node XmlNode selNode = xmlDoc.SelectSingleNode( tvXML.SelectedNode.FullPath ); // add attribute XmlAttribute newAtt = xmlDoc.CreateAttribute( newAttributeName ); newAtt.Value = newAttributeValue; selNode.Attributes.SetNamedItem( newAtt ); 

You can optionally use some of the insertion methods to the attribute in a certain position in the attribute list. Some of these methods, defined in the Attributes collection of the XmlNode class, include Prepend and Append , which add the attribute to the beginning or end of the attribute list, respectively, and InsertAfter and InsertBefore , which place the node in the corresponding position of an attribute retrieved using the Item property:

 
 // add to beginning of attribute list selNode.Attributes.Prepend( newAtt ); // add to end of attribute list selNode.Attributes.Append( newAtt ); // insert before middle element selNode.Attributes.InsertBefore(     newAtt,selNode.Attributes[selNode.Attributes.Count/2]); // insert after middle element selNode.Attributes.InsertAfter(     newAtt,selNode.Attributes[selNode.Attributes.Count/2]); 

Creating new elements or any of the other XML constructs is similar to attribute creation, but there is no need to add the newly created item to a collection. Instead, you simply select the node that will be used as the reference and call one of the insert methods that allows you to place the newly created element at a position relative to the reference node.

Comments

If you use XML extensively in your application or at least plan to, you'll eventually need to programmatically add new data into the XML document. XML files contain several individual "pieces" to them, and each one has a corresponding creation method defined in the XmlDocument class. These methods generally use string parameters for any pertinent data corresponding to the node being created. As an example, the CreateComment method uses a single string that, as you might have guessed, is the text of the comment to insert. The CreateXmlDeclaration method uses three strings, corresponding to the version, the encoding, and the value of the standalone attribute, which is generally set to null .

Each one of these methods returns a custom data type. However, these data types are ultimately derived from XmlNode so they can be used in the insertion functions defined in XmlDocument , as explained earlier.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net