Blog Archives

Serializing and DeSerializing XML Objects in .Net

these two simple extension methods with have you switching between XML and objects in no time

First off, it is pretty well known that if you have any Object and want to convert it into an XML strong, you can use the XmlSerializer to do so. I encapsulated the process is a simple extension method that run from any object – this will work as long as the object has a constructor with no inputs (ie: new SomeObject()) as that is the constraint passed up from XmlSerializer. Here is the method for converting from an object to an xml string:

public static string ToXmlString(this Object o)
{
    StringWriter xml = new
        StringWriter(new StringBuilder());
    XmlSerializer xS = new XmlSerializer(o.GetType());
    xS.Serialize(xml, o);
    return xml.ToString();
}

With this, now all you need to do is:

MyObject obj = new MyObject();
// a bunch of stuff here...
// now I want the xml representation of this:
string xml = obj.ToXmlString();

Now to go backwards, you can use the similar Deserialize along with the XmlSerializer with this method:

public static T XmlToObject<T>(this string s)
{
    var xR = XmlReader.Create(new
        StringReader(s));
    XmlSerializer xS = new XmlSerializer(typeof(T));
    T obj = (T)xS.Deserialize(xR);
    return obj;
}

It is important to notice that this is taking in a raw xml string, which would not be web safe. If you were taking in data from a web source, you would want to employ HttpUtility.UrlDecode(s) instead of just s above.
Now if you want to turn your above string ‘xml’ into an object again, simply call it like this:

MyObject obj2 = xml.XmlToObject<SomeObject>();

ServerInfo – Easily scan a Machine/Server Farm for Information

new open-source project in Asp.Net MVC 2

More than once I have been asked what databases are on what server, if server X is running application Y or what websites server ABC is running. These are are relatively simple things to accomplish, and there are tools available to get this information, but this is an extremely simple and portable solution – all the data is kept in xml, so there is no need to install a backend.

All that it is required to get all this information is to enter ip addresses and the user has the rights to scan the machines requested.

In addition, this can keep track of all of the owners of the machines and has a GUI for running WMI Queries, which is extremely powerful if you know how to use it.

This is written with Asp.Net MVC 2, C# and xml; it requires .Net 4.0 framework. I will be updating this to MVC 3/Razor in the near future.


Video – Getting Started with LINQ in .NET 3.5

A few weeks back my company offered a free online webinar on LINQ technologies to help developers more easily make the transition to LINQ.  While there was a great turn out at the webinar, I received several emails from people who couldn’t attend asking if I could provide a video recording of the webinar.  It turns out that the audio for the recording wasn’t up to my standards so I put together a video that provides an introductory look at different LINQ technologies including:

  • Lambda Expressions
  • Extension Methods
  • LINQ to Objects
  • LINQ to XML
  • LINQ to SQL
  • LINQ to Entities

You can view the video here (46 minutes).  If you’re interested in additional video tips or want to know about future webinars on .NET, SharePoint and Silverlight topics you can sign-up for the video tips newsletter here.

Logo

For more information about onsite, online and video training, mentoring and consulting solutions for .NET, SharePoint or Silverlight please visit www.TheWahlinGroup.com

XML Notepad 2007 is a Simple, Smart Editor for Web Code [Downloads]

Windows only: If you’re delving into XML programming for the first time, or want to tweak a few software files, Microsoft’s XML Notepad 2007 may be the no-nonsense editor you’re looking for.

It's not a beginner's tool due to lack of features or context—we're just assuming that most full-time or experienced programmers have their own platform preference for editing all sorts of code. XML Notepad 2007 is themed around the same kind of just-the-text-ma'am simplicity of Windows' Notepad, but does add relevant features for markup language hacking.

The left-hand pane breaks an XML document into a tree view of classes, tags, and their key values. All the text is color-matched to its identifier on the left, and writing in Tree View allows you to leave all the tag containers behind. If you want to see what your raw XML looks like, click “XSL Output,” and you’ll see what Internet Explorer would see when checking out the file. Copying and pasting are modified to ensure the nodes you shuffle around keep their contexts, and, well, that’s about it for basic features. There’s more to find in XML Notepad, but nothing more is required to get started.

XML editing gives one all kinds of powers over a system. We’ve detailed how XML tweaking can get your Firefox passwords into KeePass, help you roll your own timeline, and, to get your next level-up, build your own Firefox extension.

XML Notepad 2007 is a free download for Windows systems only.





WP Like Button Plugin by Free WordPress Templates