Archive for Uncategorized

Fun with Pseudocode

A long time ago, I saw this on bash.org:

A programmer started to cuss
Because getting to sleep was a fuss
As he lay there in bed
Looping ’round in his head
was: while(!asleep()) sheep++

It inspired me to name my blog foreach(bill) paywith(skill), which was intended to “pseudocodify,” if you will, the expression, “Skills to pay the bills.”

Recently I asked the audience of StackOverflow.com if they knew of any others. Here’s a few of my favorites, some of them with my slight modifications.

Shakespeare:

question = 2*b || !(2*b)

Working for the man:

class Employee
{
	long hours;
	short lunch;
	byte pay;
}

A Star Wars reference to Yoda, who says, “Try not. Do… or do not. There is no try.”:

do() || !do()
// try()

A classic:

while(!(succeed = try());

From Futurama, in BASIC:

10 SIN
20 GOTO HELL

Ah, to be a kid again.

if (happy && know(it)) hands.clap();

Comments (1)

Recursively processing XML elements in C#

Recursion can be tricky. But when you’ve nailed it, it’s damn sexy. Below is a basic outline for recursively processing an XML document for all its child elements, and keeping track of what depth the element is at (how many ancestors, or parents, are above it):

void Process(XElement element, int depth)
{
	// For simplicity, argument validation not performed
	
	if (!element.HasElements)
	{
		// element is child with no descendants
	}
	else
	{
		// element is parent with children
		
		depth++;

		foreach (var child in element.Elements())
		{
			Process(child, depth);
		}

		depth--;
	}
}

To begin processing a document, pass in the root element and a depth of 0:

Process(XDocument.Load(@"C:\test.xml").Root, 0);

Here’s an example of recursively displaying an XML document:


void Process(XElement element, int depth)
{
	// For simplicity, argument validation not performed

	if (!element.HasElements)
	{	
		Console.WriteLine
		(
			string.Format
			(
				"{0}<{1}>{2}</{1}>", 
				"".PadLeft(depth, '\t'), // {0}
				element.Name.LocalName,  // {1}
				element.Value			 // {2}
			)
		);
	}
	else
	{
		Console.WriteLine
		(
			"".PadLeft(depth, '\t') + // Indent to show depth
			"<" + element.Name.LocalName + ">"
		);

		depth++;

		foreach (XElement child in element.Elements())
		{
			Process(child, depth);
		}

		depth--;
		
		Console.WriteLine
		(
			"".PadLeft(depth, '\t') + // Indent to show depth
			"</" + element.Name.LocalName + ">"
		);
	}
}

Processing an XML file recursively in using extension methods and lambdas:

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        XDocument.Load(@"C:\test.xml").Root.RecursivelyProcess
        (
            // Element with no children reach

            new Action<XElement, int>((child, depth) =>
            {
                // Example of something to do with the child

                Console.WriteLine
                (
                    string.Format
                    (
                        "{0}<{1}>{2}</{1}>",
                        "".PadLeft(depth, '\t'), // {0}
                        child.Name.LocalName,    // {1}
                        child.Value			     // {2}
                    )
                );
            }),

            // Element with children reached

            new Action<XElement, int>((parent, depth) =>
            {
                // Example of something to do with the parent open

                Console.WriteLine
                (
                    "".PadLeft(depth, '\t') // Indent to show depth
                    + "<" + parent.Name.LocalName + ">"
                );
            }),

            // Finished processing element with children

            new Action<XElement, int>((parent, depth) =>
            {
                // Example of something to do with the parent close

                Console.WriteLine
                (
                    "".PadLeft(depth, '\t') // Indent to show depth
                    + "</" + parent.Name.LocalName + ">"
                );
            })
        );

        Console.Read();
    }
}

/* Alternatively, for clarity, see this implimentation:
 *
class Program
{
    static void Main(string[] args)
    {
        XDocument.Load(@"C:\test.xml").Root.RecursivelyProcess
		(
			Program.ProcessChild,
			Program.ProcessParentOpen,
			Program.ProcessParentClose
		);

        Console.Read();
    }

    static void ProcessChild(XElement child, int depth)
    {
        Console.WriteLine
        (
            string.Format
            (
                "{0}<{1}>{2}</{1}>",
                "".PadLeft(depth, '\t'), // {0}
                child.Name.LocalName,    // {1}
                child.Value			     // {2}
            )
        );
    }

    static void ProcessParentOpen(XElement parent, int depth)
    {
        Console.WriteLine
        (
            "".PadLeft(depth, '\t') // Indent to show depth
            + "<" + parent.Name.LocalName + ">"
        );
    }

    static void ProcessParentClose(XElement parent, int depth)
    {
        Console.WriteLine
        (
            "".PadLeft(depth, '\t') // Indent to show depth
            + "</" + parent.Name.LocalName + ">"
        );
    }
}
*/

/// <summary>
/// Extension methods for the .NET 3.5 System.Xml.Linq namespace
/// </summary>
public static class XExtensions
{
    /// <summary>
    /// Recursively perform operations on a XML element.
    /// </summary>
    /// <param name="element"></param>
    /// <param name="childAction">
    /// What to do when an element with no children is reached.
    /// The XElement is the child element, the int is the depth.
    /// To do nothing, pass null.
    /// </param>
    /// <param name="parentOpenAction">
    /// What to do when an element with children is reached.
    /// The XElement is the parent element, the int is the depth.
    /// To do nothing, pass null.
    /// </param>
    /// <param name="parentCloseAction">
    /// What to do when finished processing element with children.
    /// The XElement is the parent element, the int is the depth.
    /// To do nothing, pass null.
    /// </param>
    public static void RecursivelyProcess
    (
        this XElement element,
        Action<XElement, int> childAction,
        Action<XElement, int> parentOpenAction,
        Action<XElement, int> parentCloseAction
    )
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        element.RecursivelyProcess
        (
            0,
            childAction,
            parentOpenAction,
            parentCloseAction
        );
    }

    private static void RecursivelyProcess
    (
        this XElement element,
        int depth,
        Action<XElement, int> childAction,
        Action<XElement, int> parentOpenAction,
        Action<XElement, int> parentCloseAction
    )
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        if (!element.HasElements)
        {
            // Reached the deepest child

            if (childAction != null)
            {
                childAction(element, depth);
            }
        }
        else
        {
            // element has children

            if (parentOpenAction != null)
            {
                parentOpenAction(element, depth);
            }

            depth++;

            foreach (XElement child in element.Elements())
            {
                child.RecursivelyProcess
                (
                    depth,
                    childAction,
                    parentOpenAction,
                    parentCloseAction
                );
            }

            depth--;

            if (parentCloseAction != null)
            {
                parentCloseAction(element, depth);
            }
        }
    }
}

Comments (11)

Get the XPath to an XML Element (XElement)

Problem

Given an XElement, what is its XPath?

Preface

Many XPaths can be used that will select an XML element. To select one and only one element, indexes need to be used. For example, given the following XML data:

<people>
	<person>
			<name>
				<first>Chris</first>
			</name>
		</person>
	<person>
			...
</people>

The XPath “/people/person” will select all the “person” elements that are children to “people.” To refer to the first “person” element, its index needs to be used in the XPath.

Like an array, an [index] can be used to refer to a specific node. In XPath, indexes begin at 1, not 0. To select the first “person” element, then, the XPath “/people/person[1]” must be used.

I think of this as an “absolute” XPath, because it points directly to one specific element, and is not relative. Therefore, when I ask, “given an element, what is its XPath?”, I am referring to its absolute XPath—an XPath expression that will always return a specific element and its children, if it exists.

Solution

The following code snippet uses extension methods to get an absolute XPath to an XElement:

using System;
using System.Linq.Xml;

/// <summary>Extension methods for the .NET 3.5 System.Xml.Linq namespace</summary> 
public static class XExtensions
{
    /// <summary> 
    /// Get the absolute XPath to a given XElement
    /// (e.g. "/people/person[6]/name[1]/last[1]").
    /// </summary> 
	/// <param name="element"> 
	/// The element to get the index of.
	/// </param> 
    public static string AbsoluteXPath(this XElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
 
        Func<XElement, string> relativeXPath = e =>
        {
            int index = e.IndexPosition();
            string name = e.Name.LocalName;
 
            // If the element is the root, no index is required
 
            return (index == -1) ? "/" + name : string.Format
            (
                "/{0}[{1}]",
                name, 
                index.ToString()
            );
        };
 
        var ancestors = from e in element.Ancestors()
                        select relativeXPath(e);
 
        return string.Concat(ancestors.Reverse().ToArray()) + 
               relativeXPath(element);
    }
 
    /// <summary> 
    /// Get the index of the given XElement relative to its
    /// siblings with identical names. If the given element is
    /// the root, -1 is returned.
    /// </summary> 
    /// <param name="element"> 
    /// The element to get the index of.
    /// </param> 
    public static int IndexPosition(this XElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
 
        if (element.Parent == null)
        {
            return -1;
        }
 
        int i = 1; // Indexes for nodes start at 1, not 0
 
        foreach (var sibling in element.Parent.Elements(element.Name))
        {
            if (sibling == element)
            {
                return i;
            }
 
            i++;
        }
 
        throw new InvalidOperationException
            ("element has been removed from its parent.");
    }
}

Comments (2)

Wallpapers for freelancers, entrepreneurs, consultants, etc.

Ran across a few cool wallpapers on a site about freelance work while checking out a Photoshop tutorial site that was mentioned by an entrepreneur.

There’s high resolution jpgs of four different messages on simple contrasting backgrounds. The four read: “Think outside the cubicle,” “Nine to five? I don’t think so!,” “Stop the pain, be the boss!,” and “Hired gun: skills to pay the bills.”

The designs are great. I’m going to have to start rotating these on my work laptop for motivation to do something for myself also as important as what I’m doing for my company.

Comments (2)