Inserting, Updating, and Deleting Nodes

only for RuBoard

A frequently asked question on various XML mailing lists is "How do I insert a node into an XML document using an API?" The answer is surprisingly easier than what you'd think.

To update documents, you need to return to the XmlNode and XmlDocument classes. Table 6.7 shows that the XmlDocument class contains methods for creating nodes, such as CreateAttribute and CreateElement . You can use these methods in the same way that they are used with the MSXML Parser.

The .NET XML classes differ slightly from the MSXML classes. For example, text nodes can be created as children of elements simply by setting the Text property of the IXMLDOMElement interface. In the XmlDocument class in .NET, a text node must be specifically created and appended as a child of the element.

The following example loads an existing XML document and appends XML to the document:

 <%@ Page Language="vb" %>  <%@ Import Namespace="System.Xml" %>  <script language="visualbasic" runat="server">      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) graphics/ccc.gif Handles MyBase.Load          'Put user code to initialize the page here          Dim doc As XmlDocument = New XmlDocument()          doc.Load(Server.MapPath("sample.xml"))          Dim root As XmlElement = doc.DocumentElement          Dim customer As XmlNode = root.AppendChild (doc.CreateElement("Customer"))          Dim attrib As XmlAttribute = doc.CreateAttribute("id")          attrib.Value = "VBDNA"          customer.Attributes.SetNamedItem(attrib)          Dim companyName As XmlElement = customer.AppendChild graphics/ccc.gif (doc.CreateElement("CompanyName"))          companyName.AppendChild(doc.CreateTextNode ("Kirk Allen Evans Consulting, Inc"))          Dim contact As XmlElement = customer.AppendChild (doc.CreateElement("Contact"))          Dim tempNode As XmlElement = contact.AppendChild (doc.CreateElement("FirstName"))          tempNode.AppendChild(doc.CreateTextNode("Kirk Allen"))          tempNode = contact.AppendChild(doc.CreateElement("LastName"))          tempNode.AppendChild(doc.CreateTextNode("Evans"))          tempNode = contact.AppendChild(doc.CreateElement("Title"))          tempNode.AppendChild(doc.CreateTextNode("President"))          Response.ContentType = "text/xml"          Response.Clear()          doc.Save(Response.OutputStream)      End Sub  </script> 

The output from this example is shown in Figure 6.8.

Figure 6.8. Outputting an XML document directly to the HTTP response.
graphics/06fig08.gif

As you can see, the code for creating XML from scratch is straightforward, but not as straightforward as using the XmlTextWriter class. The XmlTextWriter is more useful to create XML documents from scratch, while the XmlDocument class is better suited to update the contents of a document.

However, you can mix the implementations . In the following code, you are using the XmlTextWriter to write to a stream in memory. Then the contents of the stream are used to form a new DOM document that contains only the newly created XML nodes. This DOM document is then imported into the new DOM document and appended to the document element:

 <%@ Page Language="vb" Debug="true"%>  <%@ Import Namespace="System.Xml" %>  <script language="visualbasic" runat="server">      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) graphics/ccc.gif Handles MyBase.Load          Dim memStream As IO.MemoryStream = New IO.MemoryStream()          Dim writer As XmlTextWriter          writer = New XmlTextWriter(memStream, System.Text.Encoding.UTF8)          writer.Formatting = Formatting.Indented          writer.WriteStartElement("Customer")          writer.WriteAttributeString("id", "VBDNA")          writer.WriteElementString("Companyname", "Kirk Allen Evans Consluting, Inc.")          writer.WriteStartElement("Contact")          writer.WriteElementString("FirstName", "Kirk Allen")          writer.WriteElementString("Lastname", "Evans")          writer.WriteElementString("Title", "President")          writer.WriteEndElement()          writer.WriteEndElement()          writer.Flush()          memStream.Position = 0          'Load a DOM document using the contents of the memory stream          Dim sourceDoc As XmlDocument = New XmlDocument()          sourceDoc.Load(memStream)          dim targetDoc as XmlDocument = New XmlDocument()          targetDoc.Load(Server.MapPath("sample.xml"))          Dim node As XmlNode = targetDoc.ImportNode (sourceDoc.DocumentElement,True)          targetDoc.DocumentElement.AppendChild(node)          Response.ContentType = "text/xml"          Response.Clear()          targetDoc.Save(Response.OutputStream)          On Error Resume Next          memStream.Close()          writer.Close()      End Sub  </script> 

The output from this example is the same as what's shown in Figure 6.6, but the code is cleaner and easier to understand.

Now that you have seen how to read documents, navigate through them, and change their contents, look at transforming an XML structure into a new structure. XSLT was examined in Chapter 3, "XML Presentation," from the perspective of using the XSLT elements. The next section looks at the objects in the System.Xml namespace that facilitate XSLT transformations.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

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