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
Here is a nice method of getting the primary key from a table. Basicly what I do here is obtanning the first available key to insert new rows with this obtained key.
First we set the instance of the connection with the database and then we create a sql command. After this, we open the connection and execute the command directly to the database. If everything executes correctly, we will obtain the first available key. If not, an exception is going to be raised.
In this very short article I will show you the easiest way to add anew text line on a txt file using C# .NET.
I was researching on this matter very hard. First I started to read the entire file to the end and then add the new line. There is a small problem on doing this: it might work at compilation time but when you execute the application, you will see an error that says that two processes cannot run at the same time. Sound fair. Then I started working with locks. There are a couple of locks, the lock when you read and the lock when you write. This is good stuff to talk later but not right now.
So what is the easiest solution to this matter, here it is:
public void AddNewLine(String text) { File.AppendAllText("C:\\Logs.txt", text + Environment.NewLine); }
Yes, just like that. And if you want to read the txt file, you could do something like this:
publicString ReadAll() {
StreamReader sr = new StreamReader("C:\\Logs.txt"); String allText = sr.ReadToEnd(); return allText; }
You may saw in the previous post, a couple of exceptions we can use that are provided by the .Net Framework derived from the System.Exception class. Those two exceptions were: The StackOverflowException and the NullReferenceException.
Now I will describe to you the OutOfMemoryException and the DivideByZeroException.
The OutOfMemoryException
This exception is thrown by the CLR when your machine runs out of memory. Every time you create an object using new, some memory will be reserved for it. In case there is now enough memory, the exception will be triggered.
Let´s write an example now. Supose you want to create a Hashtable that allocated a large capacity. If there is no memory to do this, the CLR throws the exception.
Take a look at this C# example:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
classProgram
{
staticvoidMain(string[] args)
{
try
{
Hashtable hatb = newHashtable(91230000);
}
catch (OutOfMemoryException)
{
Console.WriteLine("CLR out of memory");
}
}
}
}
Console:
The DivideByZeroException
This exception is quite simple: when you are dividing by 0, the CLR throws this exception.
Take a look at this example: