//Monday 17 December 2007

Toolset (Part 2) - HTML and CSS

W3C Markup Validation Service
Visual Studio will validate your HTML but this won't tell you if your final page is compliant with W3C Standards since server controls may output invalid HTML or you may write out HTML in your code behind.

http://validator.w3.org/

I usually use the Validate by Direct Input tab because often your site won't be publicly accessible during development. I just View Source and copy and paste the HTML in manually.

Having valid (X)HTML is not necessarily a requirement for intranet sites/applications because often a particular platform is being targeted. Internet Explorer (certainly pre v7) tends to be quite forgiving of errors in your markup. However all public websites should be valid to ensure they are accessible to as many users as possible.

Internet Explorer Developer Toolbar
This is a toolbar addin for Internet Explorer that helps you to diagnose HTML/CSS problems. Particularly useful is the function that allows you to select HTML elements on the page. This is good for analyzing layout problems to identify which element is giving you problems. You can also edit the CSS of elements on the fly to allow you to try out attributes like widths and heights which can be a lot quicker than going through lots of cycles of editing the code, refreshing the page, etc.

Internet Explorer Developer Toolbar

Firebug
This is an add-in for Firefox that does basically the same thing as the above add-in for Internet Explorer. Additionally, you can use it to debug Javascript.

Firebug

In combination these two add-ins usually allow me to diagnose my browser-specific CSS or Javascript issues.

//Monday 10 December 2007

Toolset (Part 1) - SysInternals

I thought I'd do a regular post on the free tools I often use in development. Some are nice to have, some I now find completely indispensable.

The SysInternals website (acquired by Microsoft in 2006) provides a number of tools which can be very useful to help developers diagnose system problems.

http://technet.microsoft.com/en-us/sysinternals/default.aspx

Here is a brief summary of some of the most useful:

Process Explorer
This tool allows you to identify what process has a particular file locked. This can be useful when you are trying to delete a file and you keep getting an Access Denied error. If you run ProcessExplorer and select Find...Find Handle or dll... and enter the file or folder you are having problems with it will display the process that has it locked.

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

FileMon
This tool is useful for identifying the source of access denied errors that you suspect are the result of access rights problems. Often you can get access denied errors from a web application when trying to access certain files and it will turn out that you are not running as the user you expected to be. FileMon allows you to monitor all file activity on your PC. Consequently the log can fill up pretty quickly as such you will need to filter your results to the application you are interested in e.g. aspnet_wp.exe (or w3wp.exe on IIS 6).

http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx

RegMon
This tool allows you to diagnose access problems that effect the registry. Similar to FileMon above you will need to filter the results to the application you are interested in e.g. aspnet_wp.exe (or w3wp.exe on IIS 6).

http://technet.microsoft.com/en-us/sysinternals/bb896652.aspx

ProcessMonitor
According to the sysinternals website ProcessMonitor replaces FileMon and RegMon for Windows 2000 and upwards and contains a number of enhancements. I haven't used this before but it might be worth checking out.

http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

//Sunday 25 November 2007

Xml Serialization in C#

In a number of recent projects we have had to expose web services to third parties. This requires discipline in agreeing and sticking to a XML schema for your communication.

BaseSerializableObject
To make sure our classes keep in step with our schemas we utilise a base serializable object class which provides methods for converting our objects to and from XML. Each object then implements a FromXml method that validates the XML against the appropriate schema before deserialization.
    public XmlDocument ToXml()
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(sb, settings);
XmlSerializer serializer = new XmlSerializer(this.GetType());
serializer.Serialize(writer, this);
XmlDocument output = new XmlDocument();
output.LoadXml(sb.ToString());
return output;
}

protected static SerializableObject FromXml(Type type, XmlDocument input, XmlSchema schema)
{
byte[] data = Encoding.Unicode.GetBytes(input.OuterXml);
using (MemoryStream ms = new MemoryStream(data))
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas.Add(schema);
XmlReader reader = XmlReader.Create(ms, settings);
XmlDocument validatingDoc = new XmlDocument();
// This will throw an error if the xml does not validate against the schema
validatingDoc.Load(reader);
// If we got this far then deserialize
ms.Position = 0;
XmlSerializer serializer = new XmlSerializer(type);
return (SerializableObject)serializer.Deserialize(ms);
}
}
Attributes
To enable you to dictate the way in which your objects are serialized you can specify XML attributes above your class declaration and properties.
  • Serializable
    This attribute is essential for your class to be serializable and must appear above your class declaration.
  • XmlRoot
    This attribute allows you to specify information about the root element of the XML document that will be the output of your serialization.
  • XmlAttribute
    This attribute allows you to specify the way in which a property of your object is serialized as an XML element.
  • XmlElement
    This attribute allows you to specify the way in which a property of your object is serialized as an XML element.
  • XmlIgnore
    This attribute tells the serializer not to include the property in the serialization.
  • XmlArray, XmlArrayItem
    These attributes tells the serializer how to name array elements.

Example

The following is a rather contrived example to demonstrate these concepts:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSerialization
{
[Serializable, XmlRoot(ElementName = "objectXml")]
public class MySerializableObject : SerializableObject
{
private int _integerOne;
private int _integerTwo;
private int _integerThree;
private int _integerFour;
private string _stringOne;
private string _stringTwo;
private string _stringThree;
private string _stringFour;
private MySerializableObject[] _children;

public MySerializableObject()
{
}

public MySerializableObject(int integerOne,
int integerTwo,
int integerThree,
int integerFour,
string stringOne,
string stringTwo,
string stringThree,
string stringFour)
{
_integerOne = integerOne;
_integerTwo = integerTwo;
_integerThree = integerThree;
_integerFour = integerFour;
_stringOne = stringOne;
_stringTwo = stringTwo;
_stringThree = stringThree;
_stringFour = stringFour;
}

public static MySerializableObject FromXml(XmlDocument objectXml)
{
// Load embedded Xml schema from assembly
XmlSchema schema = XmlSchema.Read(Assembly.GetAssembly(typeof(MySerializableObject)).GetManifestResourceStream("XmlSerialization.schemas.MySerializableObject.xsd"), null);
// Deserialize object from XML provided (if XML passes validation)
MySerializableObject obj = (MySerializableObject)SerializableObject.FromXml(typeof(MySerializableObject), objectXml, schema);
return obj;
}

[XmlElement(ElementName = "numberTwo", Order = 4)]
public int IntegerOne
{
get
{
return _integerOne;
}
set
{
_integerOne = value;
}
}

[XmlIgnore]
public int IntegerTwo
{
get
{
return _integerTwo;
}
set
{
_integerTwo = value;
}
}

[XmlElement(ElementName = "numberOne", Order = 3)]
public int IntegerThree
{
get
{
return _integerThree;
}
set
{
_integerThree = value;
}
}

[XmlAttribute(AttributeName = "integerAttribute")]
public int IntegerFour
{
get
{
return _integerFour;
}
set
{
_integerFour = value;
}
}

[XmlElement(ElementName = "textTwo", Order = 2)]
public string StringOne
{
get
{
return _stringOne;
}
set
{
_stringOne = value;
}
}

[XmlIgnore]
public string StringTwo
{
get
{
return _stringTwo;
}
set
{
_stringTwo = value;
}
}

[XmlElement(ElementName = "textOne", Order = 1)]
public string StringThree
{
get
{
return _stringThree;
}
set
{
_stringThree = value;
}
}

[XmlAttribute(AttributeName = "stringAttribute")]
public string StringFour
{
get
{
return _stringFour;
}
set
{
_stringFour = value;
}
}

[XmlArray(ElementName = "kids", Order = 5)]
[XmlArrayItem(ElementName = "kid")]
public MySerializableObject[] Children
{
get
{
return _children;
}
set
{
_children = value;
}
}
}
}

//Monday 5 November 2007

Welcome to the sourcemotion blog

Welcome to the new sourcemotion blog. I'm going to be posting articles about IT, specifically web development using Microsoft technologies. I hope people find it useful. I think it will be useful and interesting for me to have a log of the sorts of things I have been working on and the solutions we have come up with.

SyntaxHighlighter