C# Workshop – Libraries

Standard

Libraries

Throughout the examples used with dotnetfiddle.net the using has been put at the top to include a namespace, this is taking advantage of the .NET Framework which features many kinds of namespace that can be used in an application that add features both complex and simple so many things don’t need to be done again if they’ve been done already and many of these are contained in the .NET Framework class Libraries such as System and System.Text used in previous examples.

To use another namespace in dotnetfiddle.net enter the following:

using System;
using System.Text;
using System.Xml;

public class Demo
{
	public class Person
	{		
		public string Forename { get; set; }
		public string Surname { get; set; }
	}
		
	public static void Main()
	{
		Person item = new Person() 
		{ 
			Forename = "John", 
			Surname = "Smith" 
		};			
		StringBuilder output = new StringBuilder();
		using(XmlWriter writer = XmlWriter.Create(output))
		{
			writer.WriteStartElement("person");
			writer.WriteStartAttribute("forename");
			writer.WriteValue(item.Forename);
			writer.WriteEndAttribute();
			writer.WriteStartAttribute("surname");
			writer.WriteValue(item.Surname);
			writer.WriteEndAttribute();
			writer.WriteEndElement();
		}
		Console.WriteLine(output.ToString());
	}
}

Namespaces used in the previous examples were System, System.Text and System.Xml – the first is where the main and common features such as the Console Methods that have been used like Console.WriteLine. System.Text is used for StringBuilder which has also been used before, in this case it’s used in combination with System.Xml to create an XmlWriter – this is used to create an XML document which is a way of storing data that can be read easily by a program or in the case written. There’s another use of using here to create the XmlWriter to make sure it’s created at the top part and then closed or Disposed when finished.
Another example is to use XmlReader to read in XML and take advantage of another namespace which is System.IO which allows input and output methods to be used in this case a StringReader to read some existing XML as an input for an application.

In dotnetfiddle.net enter the following example:

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

public class Demo
{
	public class Person
	{		
		public string Forename { get; set; }
		public string Surname { get; set; }
	}
		
	public static void Main()
	{
		Person item = new Person();

		string input = "<person forename='John' surname='Smith'/>";
		
		using(XmlReader reader = XmlReader.Create(new StringReader(input)))
		{
			reader.ReadToFollowing("person");
			item.Forename = reader.GetAttribute("forename");
			item.Surname = reader.GetAttribute("surname");
		}
		Console.WriteLine("Forename:" + item.Forename);
		Console.WriteLine("Surname:" + item.Surname);
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s