13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Console.WriteL<strong>in</strong>e(nodeReader.Value);}}The while loop visits all the nodes belong<strong>in</strong>g to the specified <strong>XML</strong> DOM subtree. Thenode reader class is <strong>in</strong>itialized us<strong>in</strong>g the XmlNode object that is the root of the <strong>XML</strong>DOM subtree.Updat<strong>in</strong>g Text and MarkupOnce an <strong>XML</strong> document is loaded <strong>in</strong> memory, you can enter all the needed changes bysimply access<strong>in</strong>g the property of <strong>in</strong>terest and modify<strong>in</strong>g the underly<strong>in</strong>g value. Forexample, to change the value of an attribute, you proceed as follows:// Retrieve a particular node and update an attributeXmlNode n = root.SelectS<strong>in</strong>gleNode("days");n.Attributes["module"] = 1;To <strong>in</strong>sert many nodes at the same time and <strong>in</strong> the same parent, you can exploit a littletrick based on the concept of a document fragment. In essence, you concatenate all thenecessary markup <strong>in</strong>to a str<strong>in</strong>g and then create a document fragment, as shown here:XmlDocumentFragment df = doc.CreateDocumentFragment();df.InnerXml = "ValueAnotherValue";parentNode.AppendChild(df);Set the InnerXml property of the document fragment node with the str<strong>in</strong>g, and then addthe newly created node to the parent. The nodes def<strong>in</strong>ed <strong>in</strong> the body of the fragmentwill be <strong>in</strong>serted one after the next.In general, when you set the InnerXml property on an XmlNode-based class, anydetected markup text will be parsed, and the new contents will replace the exist<strong>in</strong>gcontents. For this reason, if you want simply to add new children to a node, passthrough the XmlDocumentFragment class, as described <strong>in</strong> the previous paragraph, andavoid us<strong>in</strong>g InnerXml directly on the target node.Detect<strong>in</strong>g ChangesCallers are notified of any changes that affect nodes through events. You can set eventhandlers at any time and even prior to load<strong>in</strong>g the document, as shown here:XmlDocument doc = new XmlDocument();doc.NodeInserted += new XmlNodeChangedEventHandler(Changed);doc.Load(fileName);If you use the preced<strong>in</strong>g code, you will get events <strong>for</strong> each <strong>in</strong>sertion dur<strong>in</strong>g the build<strong>in</strong>gof the <strong>XML</strong> DOM. The follow<strong>in</strong>g code illustrates a m<strong>in</strong>imal event handler:void Changed(object sender, XmlNodeChangedEventArgs e){Console.WriteL<strong>in</strong>e(e.Action.ToStr<strong>in</strong>g());}Note that by design <strong>XML</strong> DOM events give you a chance to <strong>in</strong>tervene be<strong>for</strong>e and after anode is added, removed, or updated.187

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!