.NET goes open source and cross platform with Mono
In the last blog i talked about MonoDevelop an open source cross platform IDE for .NET development. This cross platform .NET development was only possible due to the Mono Framework.
Mono is a software platform designed to allow developers to easily create cross platform applications. It is an open source implementation of Microsoft’s .Net Framework based on the ECMA standards for C# and the Common Language Runtime. We feel that by embracing a successful, standardized software platform, we can lower the barriers to producing great applications for Linux.
The Components
There are several components that make up Mono:
C# Compiler – The C# compiler is feature complete for compiling C# 1.0 and 2.0 (ECMA), and also contains many of the C# 3.0 features.
Mono Runtime – The runtime implements the ECMA Common Language Infrastructure (CLI). The runtime provides a Just-in-Time (JIT) compiler, an Ahead-of-Time compiler (AOT), a library loader, the garbage collector, a threading system and interoperability functionality.
Base Class Library – The Mono platform provides a comprehensive set of classes that provide a solid foundation to build applications on. These classes are compatible with Microsoft’s .Net Framework classes.
Mono Class Library – Mono also provides many classes that go above and beyond the Base Class Library provided by Microsoft. These provide additional functionality that are useful, especially in building Linux applications. Some examples are classes for Gtk+, Zip files, LDAP, OpenGL, Cairo, POSIX, etc.
The Benefits
There are many benefits to choosing Mono for application development:
Popularity – Built on the success of .Net, there are millions of developers that have experience building applications in C#. There are also tens of thousands of books, websites, tutorials, and example source code to help with any imaginable problem.
Higher-Level Programming – All Mono languages benefit from many features of the runtime, like automatic memory management, reflection, generics, and threading. These features allow you to concentrate on writing your application instead of writing system infrastructure code.
Base Class Library – Having a comprehensive class library provides thousands of built in classes to increase productivity. Need socket code or a hashtable? There’s no need to write your own as it’s built into the platform.
Cross Platform – Mono is built to be cross platform. Mono runs on Linux, Microsoft Windows, Mac OS X, BSD, and Sun Solaris, Nintendo Wii, Sony PlayStation 3, Apple iPhone. It also runs on x86, x86-64, IA64, PowerPC, SPARC (32), ARM, Alpha, s390, s390x (32 and 64 bits) and more. Developing your application with Mono allows you to run on nearly any computer in existance (details).
Common Language Runtime (CLR) – The CLR allows you to choose the programming language you like best to work with, and it can interoperate with code written in any other CLR language. For example, you can write a class in C#, inherit from it in VB.Net, and use it in Eiffel. You can choose to write code in Mono in a variety of programming languages.
–courtesy mono project
MonoDevelop opens up Mac for .NET development
As I said that I am a technology evangelist. I like new technology in .NET as well as open source. Other thing which I liked was Mac. But before MonoDevelop both were two different worlds. You could not develop a .NET application on a Mac OS X. MonoDevelop has solved most of my problem or you can say it is the new bridge between different platforms.
MonoDevelop is an opensource Integrated development environment for Linux platform, Mac OSX and Windows(to be supported in future). It allows you to develop software targeted to Mono and .NET framework. This IDE has feature like intellisense, source control integration and an integrated GUI and Web designer
MonoDevelop has recently launched the latest version of the IDE. To read more about it http://monodevelop.com/Download/MonoDevelop_2.0_Released.
If you’ve worked with Microsoft Visual Studio, you will see many similarities in MonoDevelop and will feel quite comfortable in the Mono environment. If you’re new to MonoDevelop and haven’t worked in Visual Studio, you’ll find that the learning curve is not very steep.
A new competitor for Visual Studio IDE…. eeh…lets see!!
XMLDocument Vs LINQ to XML
.NET as it had evolved has come up with different API to read and write XML data. If you are using .NET 3.0 and lower version you will have to use XMLDocument aka the classic DOM API. With .NET 3.0 Microsoft had launched Language Integrated Query(LINQ) and with this came one of the feature name the LINQ to XML.
LINQ to XML has a simple model for building XML documents by hand. Whether it be XML sources from a stream or file, or XML created on-the-fly in code there are only a few important types to know and understand. The main ones used in everyday activities are XDocument, XElement and XAttribute.
In this article we will talk about XDocument and XMLDocument.
Lets consider the following test data for our article
<root>
<child id=’123′/>
<child id=’234′/>
…
</root>
Reading the XML
XmlDocument.Load
XmlDocument.Load was the cleanest and easiest to understand. It is necessary that you must know XPath, although the fact is I like XPath. XmlDocument does have some security concerns with XPath injection. Here is how we code to load a document
private static void XmlDocumentReader(string fileName) {
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes(“//child”);
if (nodes == null) {
throw new ApplicationException(“invalid data”);
}
foreach (XmlNode node in nodes) {
string id = node.Attributes["id"].Value;
ProcessId(id);
}
}
Another way to query a single node is given below
XmlDocument doc = XmlDocument.Load(fileName);
XmlNode node = doc.SelectSingleNodes(“/root/child[@id=’123’")
LINQ to XML
LINQ to XML was also very easy to read and understand code. XDocument.Load does read the whole document into memory before returning. Due to feature of Lambda expression it has become very easy to traverse the xml document. The query syntax is easier than XPath or XQuery for developers who do not use XPath or XQuery on a daily basis. Here is the code I used to load and search the document:
private static void XDocumentReader(string fileName) {
XDocument doc = XDocument.Load(fileName);
if (doc == null | doc.Root == null) {
throw new ApplicationException("invalid data");
}
foreach (XElement child in doc.Root.Elements("child")) {
XAttribute attr = child.Attribute("id");
if (attr == null) {
throw new ApplicationException("invalid data");
}
string id = attr.Value;
ProcessId(id);
}
}
Another way to query a single node is given below
var xd = XDocument.Load(fileName);
var domQuery =
from c in xd.Descendants("child ")
where (string)c.Attribute("id") == "123"
Writing a new XML
Both the method almost looks similar.
XmlDocument
XmlDocument xmlDoc = new XmlDocument();
// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("root");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
// Create a new <child> element and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("child");
// Set attribute name and value!
parentNode.SetAttribute("ID", "01");
xmlDoc.DocumentElement.PrependChild(parentNode);
LINQ to XML
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("root",
new XElement ("child", new XAttribute("id", "01")
)
);
Manipulating XML Data
XmlDocument
Here we have to struggle with manipulating the xml data. First take help of XPath to select the node and then use replace node to update it. Or use the feature of delete node and insert new node. Code is given below
XmlDocument doc = new XmlDocument.Load(fileName);
//Select the cd node with the matching title
XmlNode oldCd;
XmlElement root = doc.DocumentElement;
oldCd = root.SelectSingleNode("/root/child[id='1']“);
XmlElement newCd = doc.CreateElement(“child”);
newCd.SetAttribute(“id”,2);
root.ReplaceChild(newCd, oldCd);
//save the output to a file
doc.Save(fileName);
LINQ to XML
Here the code is very simple to write and understand. The coding time is also very less.
var xd = XDocument.Load(fileName);
var domQuery =
from c in xd.Descendants(“child “)
where (string)c.Attribute(“id”) == “123″
domQuery.Attribute(“id”).Value = 1
xd.save(filename)
Conclusion
Overall speaking both the API’s are equally powerful. However LINQ has made it little bit easier for developer to work with XML Data. I would say if you are using .NET 3.5 and higher you should rather go with LINQ To XML.