0% found this document useful (0 votes)
36 views

Sax Parser

The code loads an XML document, selects a node, creates and adds a new attribute to that node, then saves the changes back to the original file. Specifically, it loads the bookDetails.xml file, finds the book node for "The Great Gatsby", creates a new "publishedYear" attribute set to "1925", appends it to that book node, and saves the updated XML document back to bookDetails.xml.

Uploaded by

Naresh S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Sax Parser

The code loads an XML document, selects a node, creates and adds a new attribute to that node, then saves the changes back to the original file. Specifically, it loads the bookDetails.xml file, finds the book node for "The Great Gatsby", creates a new "publishedYear" attribute set to "1925", appends it to that book node, and saves the updated XML document back to bookDetails.xml.

Uploaded by

Naresh S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.IO;
using System.Xml;

namespace SaxParserExample
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("data.xml");

XmlNode root = doc.DocumentElement;

XmlElement newElement = doc.CreateElement("newNode");


newElement.InnerText = "new value";

root.AppendChild(newElement);

doc.Save("data.xml");
}
}
}

In this example, the XmlDocument object doc loads the data.xml file. The root node
of the document is accessed and stored in the root variable. A new XmlElement is
created using the CreateElement method of the doc object and the element's inner
text is set to "new value". The new element is then added to the end of the root
node using the AppendChild method. Finally, the changes to the document are saved
back to the data.xml file using the Save method.

using System;
using System.IO;
using System.Xml;

namespace SaxParserExample
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("bookDetails.xml");

XmlNode bookNode = doc.SelectSingleNode("//book[title='The Great


Gatsby']");
XmlAttribute newAttribute = doc.CreateAttribute("publishedYear");
newAttribute.Value = "1925";

bookNode.Attributes.Append(newAttribute);

doc.Save("bookDetails.xml");
}
}
}
In this example, the XmlDocument object doc loads the bookDetails.xml file. The
node for the book with the title "The Great Gatsby" is selected using the
SelectSingleNode method. A new XmlAttribute is created using the CreateAttribute
method of the doc object and its value is set to "1925". The new attribute is then
added to the book node using the Append method of the Attributes property. Finally,
the changes to the document are saved back to the bookDetails.xml file using the
Save method.

You might also like