MIS 21 - Introduction To Applications Development
MIS 21 - Introduction To Applications Development
Steps:
1. Create an instance of XmlDocument
2. Call the load() of XmlDocument
3. Create an instance of XmlNode and let it be a
reference to the root element. Use the
DocumentElement property of XmlDocument
Loading XML Files
Sample code
XmlDocument document = new XmlDocument();
document.Load(path);
XmlNode root = document.DocumentElement;
Tips
Put complete path for the Xml Document for proper
loading
Do not forget to instantiate the root element of the
document, all other elements will be derived from this
Display XML Elements
Use the following classes
XmlNodeList (arraylist of XmlNodes)
XmlNode
foreach(string s in songRow)
{ Console.WriteLine(s);
}
}
Display XML Elements
Itis possible to go deeper into the
document. Use the following to access
child nodes of any XmlNode:
ChildNodes, FirstChild, LastChild,
NextSibling
Attributesmay be printed by following
the same structure but using
XmlAttribute instead of XmlNode.
Add XML Elements
Use the following classes
XmlNode
XmlElement (represents the tags)
XmlTextNode (represents the value between
tags
Add XML Elements
Steps:
1. Create XmlElements to represent its children,
use CreateElement() for this
2. Create XmlTextNodes to represent the values
using CreateTextNode()
3. Associate each XmlTextNode to its respective
XmlElement using the AppendChild() of the
XmlElement objects
Add XML Elements
Steps:
4. Create an XmlElement to represent the parent
node using the CreateElement() method of the
XmlDocument object
5. Append the child XmlElement objects to the
parent XmlElement object using AppendChild()
of the parent XmlElement
6. Append the parent XmlElement object to the
root element using AppendChild() of the root
element
Add XML Elements
Sample code
XmlElement newTitle = document.CreateElement("artist");
XmlText newTitleText = document.CreateTextNode(artist);
newTitle.AppendChild(newTitleText);
...
root.AppendChild(newSong);
Add XML Elements
XmlElement and XmlText are both derived
from XmlNode, so technically an XmlNode
object may receive the objects returned by
CreateElement() and CreateTextNode()
Always append
Text values to child elements
Child elements to parent elements
Parent elements to root element
Update XML Elements
Steps:
1. Identify the XmlNode with values to
modify
2. Identify the XmlElements within the
XmlNode whose value is to be changed
3. Change the value of by using the
InnerText property.
Update XML Elements
Sample code
String searchTitle = Console.ReadLine();
XmlNodeList children = root.ChildNodes;
for (int x = 0; x < children.Count; x++)
{
XmlNode song = children[x];
string songTitle = song.ChildNodes[1].InnerText;
if (songTitle.Equals(searchTitle))
{
song.ChildNodes[0].InnerText = newArtist;
song.ChildNodes[1].InnerText = newTitle;
song.ChildNodes[2].InnerText = newAlbum;
Call
after adding, updating or deleting
XML elements
Use the same file called in
document.Load() to overwrite the old
data with the new data
Creating an XML document
Sometimes, there is a need to Create
an XML file
Steps:
1. Create the document
2. Create the root element
3. Append the root to the document
4. Append everything else to the root
Creating an XML document
Sample Code
static void Main(string[] args)
{
XmlDocument document = new XmlDocument();
XmlNode root = document.CreateElement("songs");
document.AppendChild(root);