home *** CD-ROM | disk | FTP | other *** search
Wrap
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%> <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" --> <h4>How Do I...Edit XML with the XmlNavigator?</h4> <div class="indent" style="width:660"> This sample illustrates how to edit XML documents with an XmlNavigator using the DocumentNavigator and DataDocumentNavigator classes. This sample follows on from the <a href="NavigateXmlDocument.aspx">How Do I...Navigate XML with the XmlNavigator?</a> topic. </div> <h4>Editing XML with the XmlNavigator</h4> <Acme:SourceRef ViewSource="/quickstart/howto/samples/Xml/EditingXmlDocument/EditingXmlDocument.src" RunSample="/quickstart/howto/samples/Xml/EditingXmlDocument/EditingXmlDocument.aspx" Icon = "/quickstart/images/genicon.gif" Caption="EditingXmlDocument.aspx" runat="server" /> <br clear="left"><br> <div class="indent" style="width:660"> Here we are going to use the editing methods on the XmlNavigator to edit the XML document. First, using an XPath expression, all the books prices are selected in the bookstore and the prices of the books are increased by 2%. Second a new book is added to the bookstore. The sample code shows the XPath expression. </div> <div class="code"> <xmp> navigator.Select ("descendant::book/price"); while (navigator.MoveToNextSelected()) { navigator.MoveToFirstChild(); Double price = Double.Parse(navigator.Value); navigator.Value = Double.Format(price * 1.02, "#.00"); navigator.MoveToParent(); } </xmp> </div> <div class="indent" style="width:660"> Here we move to the first text node (the book's price) with the <b>MoveToFirstChild</b> method, update it's value and then move back to the parent node with the <b>MoveToParent</b> method. The formatting on the string ensures that only two decimal places are written.</div> <br clear="left"><div class="indent" style="width:660"> Next we are going to insert a new book into the bookstore using the <b>Insert</b> method. This method creates a new node and inserts it at the specified position in the tree relative to the current position and moves the current node to point at this newly inserted node.</div> <br clear="left"><div class="indent" style="width:660"> The relative position is determined by the TreePosition enumeration shown in the table below.</div> <br clear="left"><br> <DIV class=indent style="width:660"> <TABLE class=table style="border-style: solid" width="418"> <TBODY> <TR> <TH width="100">TreePosition Enum</TH> <TH width="308">Description</TH> <TH width="10">Value</TH> </TR> <tr> <td height="17"><font size="1">None</font></td> <td height="17"><font size="1">No position</font></td> <td height="17"><font size="1">0</font></td> </tr> <tr> <td width="100" height="19"><font size="1">Before</font></td> <td width="308" height="19"><font size="1">The sibling immediately before the current node</font></td> <td width="10" height="17"><font size="1">1</font></td> </tr> <tr> <td width="100" height="19"><font size="1">After</font></td> <td width="308" height="19"><font size="1">The sibling immediately after the current node</font></td> <td width="10" height="17"><font size="1">2</font></td> </tr> <tr> <td width="100" height="19"><font size="1">FirstChild</font></td> <td width="308" height="19"><font size="1">The first child of the current node</font></td> <td width="10" height="17"><font size="1">3</font></td> </tr> <tr> <td width="100" height="19"><font size="1">LastChild</font></td> <td width="308" height="19"><font size="1">The last child of the current node</font></td> <td width="10" height="17"><font size="1">4</font></td> </tr> <tr> <td width="100" height="19"><font size="1">Parent</font></td> <td width="308" height="19"><font size="1">The parent of the current node</font></td> <td width="10" height="17"><font size="1">5</font></td> </tr> <tr> <td width="100" height="19"><font size="1">FirstAttribute</font></td> <td width="1000" height="19"><font size="1">The first attribute of the current node</font></td> <td width="10" height="17"><font size="1">6</font></td> </tr> <tr> <td width="100" height="19"><font size="1">LastAttribute</font></td> <td width="1000" height="19"><font size="1">The last attribute of the current node</font></td> <td width="10" height="17"><font size="1">7</font></td> </tr> </TBODY></TABLE></DIV> <br clear="left"><br><div class="indent" style="width:660"> The code sample below starts at the document node and moves thorough the XML tree inserting element and attribute nodes to insert a new book into the XML document. Once updated the XML data is written out to a new file called <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/EditingXmlDocument/&file=updatebooks.xml">updatebooks.xml</a>. <div class="code"> <xmp> // Navigate to add a new book at the start of the bookstore navigator.MoveToDocumentElement(); // Insert the new book navigator.Insert(TreePosition.FirstChild,XmlNodeType.Element,"book","",""); navigator.SetAttribute ("genre","Object-Orientated Technology"); navigator.SetAttribute ("publicationdate","1994"); navigator.SetAttribute ("ISBN","0-201-63361-2"); navigator.Insert(TreePosition.FirstChild,XmlNodeType.Element,"title","",""); navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"title","",""); navigator.Value = "Design Patterns - Elements of Reusable Object-Orientated Software"; navigator.MoveToParent(); //author element node navigator.Insert(TreePosition.After,XmlNodeType.Element,"author","",""); navigator.Insert(TreePosition.FirstChild,XmlNodeType.Element,"first-name","",""); navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"first-name","",""); navigator.Value = "Eric"; navigator.MoveToParent(); //author element node navigator.Insert(TreePosition.After,XmlNodeType.Element,"last-name","",""); navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"last-name","",""); navigator.Value = "Gamma"; navigator.MoveToParent(); //author element node navigator.MoveToParent(); //title element node navigator.Insert(TreePosition.After,XmlNodeType.Element,"price","",""); navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"price","",""); navigator.Value = "49.95"; </xmp> </div> <div class="indent" style="width:660"> Finally the changes are written to a file. </div> <div class="code"> <xmp> xmldocument.Save(m_UpdatedDocument); </xmp> </div> <H4>Summary</H4> <OL> <LI>The XmlNavigator provides a cursor style model to navigate an XML in-memory tree. <LI>The Select method applies the XPath query to the XML document. This is the best method for retrieving a set of nodes to perform editing operations on. <LI>Insert and Remove methods allow you to perform editing operations on the XML document. <LI>Editing operations can also be performed with the W3C DOM, however the use of XPath queries with the XmlNavigator makes this a more intuitive approach. </LI></OL> <!-- #include virtual="/quickstart/howto/include/footer.inc" -->