Blog posts


  • C# classes created from XML with XSD.EXE

    June 07, 2012 | Tags: Tools, XML, C#
    The XSD.EXE tool that is included in Visual Studios command line tools is a bit flawed.

    Running the command first generating an XSD scheme out of your XML file and then creating classes from it:
    xsd MyXml.xml
    xsd MyXml.xsd /classes /n:MyNameSpace.Serialization

    First of all, most likely your array elements will be declared as multi dimensional arrays. If so you'll end up with a "Unable to generate a temporary class" exception and plenty of "error CS0030: Cannot convert type 'Data[]' to 'Data'". (Data is an example return type, yours will probably be named something else, Row is an example element name.)

    The code in the generated class would look something like this:
    [System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data), IsNullable=false)]
    public Data[][] Row ...

    If you actually use multi dimensional arrays, it should really be using typeof(Data[]) instead, like this:
    [System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data[]), IsNullable=false)]
    public Data[][] Row ...

    But if you're just looking for normal one dimensional arrays it's the return type that needs to be flattened to Data[] instead of Data[][]:
    [System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data), IsNullable=false)]
    public Data[] Row ...

    Read the original solution post here.

    The second problem you may face is if your XML doesn't contains namespaces. It would occur like "<yourrootelement xmlns=""> was not expected.". To fix this, remove the empty Namespace parameters from the XmlRootAttribute in your generated classes. You'll find some alternative fixes for this problem here.

  • C# check if type is nullable

    May 07, 2012 | Tags: C#, Snippet
    This is a nice little snippet to check if a type is nullable:

    public static bool IsNullableType(Type type) {
    if(!type.IsGeneric) {
    return false;
    }
    if(type.GetGenericTypeDefinition() == typeof(Nullable<>)) {
    return true;
    }
    else {
    return false;
    }
    }

    This is a restructured version of Snippet: Check if type is Nullable (C# 2.0) or not (C#)

  • Generating GUID value with ReSharper

    May 07, 2012 | Tags: Tools, C#
    Do you need to have a new GUID generated for your code i Visual Studio? If you have ReSharper installed it´s a piece of cake.

    In your code, just write nguid and hit tab. You should now be looking at a selection of string formats for your new GUID value!

    If you do not yet have ReSharper head over and download a trial, you won't regret it :)

  • Store TFS credentials for CodePlex

    May 06, 2012 | Tags: Team Foundation Server, Open source
    Tired of always having to rewrite you credentials when starting up you CodePlex hosted project in Visual Studio? If so, just follow these steps (assuming you're using Windows 7):

    1.  Go to the Control Panel and select User Accounts and Manage your credentials.
    2. Add a Windows credential, enter tfs.codeplex.com and your login criteria (username/password). Be sure to include the domain prefix for your username.
    3. Click OK and you're done!
    Next time you go online with Visual Studio you'll be automatically logged in.

    This will also work for any other TFS, you just need to put the correct address instead of tfs.codeplex.com when adding the Windows credential.

  • Soft breaks in HTML text

    May 02, 2012 | Tags: HTML, Snippet

    If you want to hint to the web browser where to break a word when it doesn't fit on the same line there's a nice little thing called soft hyphen, available through &shy; (also &#173; or &#xAD;).


    <p>This is an example of a wordthat&shy;shouldbreak in the middle if it doesn't fit on the same line.</p>

    The above example will break the made up "wordthatshouldbreak" at the &shy; character if the whole word doesn't fit on the same line, resulting in "wordthat-shouldbreak".

    More info about line breaks and hyphenation over at W3's HTML 4.01 Specification.