In the previous post I showed you how to create a XmlDocument following some easy steps. The idea now is to read the created xml and find elements by a specific tag. Then we are going to change that value and save the edited xml.
Code:
For the first book element, we are going to add inner text:
XmlDocument xmlDoc = newXmlDocument();
//Load the Xml created previously
xmlDoc.Load(@"C:\XML\" + "xmlTest.xml");
XmlNodeList books;
books = xmlDoc.GetElementsByTagName("book-1");
//Since there is only one element with this tag, the loop is not necessary.
foreach (XmlNode node in books)
{
node.InnerText = "Great Book";
}
For the second element, we are changing the attributes values:
Here is a small post about using split function in c#. We are going to split a String by using delimiters. A delimiter will tell when to split the string. So here is a fragment of code to perform splitting.
String answer = "This$is|a|string";
char[] delimitator1 = newchar[] { '$' };
char[] delimitator2 = newchar[] { '|' };
string[] words = answer.Split(delimitator1);
Console.WriteLine("First word : " + words[0]);
//Note that now we split the second part of the string