XPath in LINQ

To use XPath in LINQ, add a reference to System.Xml.XPath. This will make the System.Xml.XPath.Extensions come into scope. Then, you can use the XPathEvaluate() method.

XPath Summary

Here’s a simple summary of XPath.

I’m including it here in case the source site goes away . . .

An XPath expresssion is a series of location steps separated by slashes (/). Each step selects a set of nodes in relation to the current node. These nodes become the current node(s) for the next step. The set of nodes selected by the expression are the nodes remaining after processing each step in order.

For example, the expression child::Part consists of a single step. This step selects the set of all Part elements that are children of the current node. The expression parent::node()/child::Part has two steps. The first step (parent::node()) selects a single node — the parent of the current node. The second step (child::Part) selects all Part elements that are children of that node. Thus, the expression identifies all Part children of the parent of the current node. In other words, it selects all Part siblings of the current node, possibly including the current node.

A step in an XPath expression consists of three parts: an axis, a node test, and zero or more predicates. The axis specifies the direction to move in the document tree (which is roughly the same as the DOM document tree). For example, the child axis says to look at all child nodes. The parent axis says to look at the parent node. The self axis says to look at the current node. The descendant axis says to look at all descendant nodes. And so on.

A node test tests whether nodes encountered along the specified axis should be selected for the next step. For example, in child::Part, child is the axis and Part is the node test. A child node satisfies the node test if it is an element with the name of Part. There are node tests that check the element, attribute, and namespace name, as well as tests that check if the node is a text, comment, or processing instruction node.

A predicate is an expression that filters nodes selected by the node test. It takes the form of an equality (=, !=, >, <, >=, <=) expression. For example, [self::text()="123"] tests if the text of the current node is "123". And [child::PartNumber/self::text()="123"] tests if the current node has a PartNumber child with text of "123". Thus, the XPath expression child::PartNumber[self::text()="123"] selects all PartNumber children of the current node with text "123". And (more informally) child::Part[child::PartNumber/self::text()="123"] selects all parts that are children of the current node and have a part number of "123".

ASP.NET MVC4 with Bootstrap 2.3.2

If you want to get Twitter Bootstrap 2.3.2 up and running with ASP.NET MVC4, there is a wonderful set of projects on NuGet for that. However, it’s not straight forward to get it installed. There are issues that have not been resolved due to the author’s time constraints (admittedly by him) in keeping up the NuGet project. Still, if you want to get an MVC4 Bootstrap 2.3.0 app going, this is a pretty good place to start inspite of the problems.

I have struggled to get it up and going and have noticed in my search for answers that others have as well. I have finally converged upon a sequence of steps that get it going. I’ve not found these all in one place, so, I’ve decided to document my approach here. I hope it works for you . . .

  1. In Visual Studio, create a new, empty MVC 4 application.
  2. Open the Library Package Manager console window and load the two Twitter Bootstrap MVC 4 and Sample packages
    1. “Tools | Library Package Manager | Package Manager Console”
    2. At the PM> prompt, type
         install-package twitter.bootstrap.mvc4 -Version 1.0.90
      and press [Enter] and wait for it to install.
    3. At the PM> prompt, type
         install-package twitter.bootstrap.mvc4.sample -Version 1.0.90
      and press [Enter] and wait for it to install
    4. Later versions have bugs that have not been fixed. 1.0.90 is a stable version
  3. Paths in the BootstrapBundleConfig are wrong and need to be fixed. Change it to something like this . . .

    bundles.Add(new StyleBundle(“~/content/css”).Include(
        “~/Content/bootstrap/bootstrap.css”,
        “~/Content/body.css”,
        “~/Content/bootstrap/bootstrap-responsive.css”,
        “~/Content/bootstrap-mvc-validation.css”
        ));

  4. The Bootstrap files need to be updated (the ones that come down in NuGet aren’t right). You’ll need to install it manually in your project.
    1. Get bootstrap 2.3.2 from here . . . click the big blue “Download Bootstrap” button.
    2. Open the download file, extract it and copy the css and js files over the existing ones in the \Content and \Scripts folders . . . watch out for the paths
  5. In the _BootstrapLayout.basic.cshtml, change the lines
            <link href=”@Styles.Url(“~/content/css”)” rel=”stylesheet”/>
    to
            @Styles.Render(“~/content/css”)

OR . . .

you can just download this project with all of the above and start from there.

Patterns of Parallel Programming

I read this answer today on Stack Overflow:

http://stackoverflow.com/a/8071784/2387067

to a question on parallel programming and getting as much work done as possible. The answer referenced this, very well written document from Microsoft:

http://www.microsoft.com/download/en/details.aspx?id=19222

In it, they describe the fundamental problems encountered in parallel programming, how they are solved and why they are solved the way they suggest. Really good, useful read.

Stephen Toub has some good books. Here’s one
http://amzn.to/14p6Hwx

var in C# . . . why?

Here is the link to Microsoft’s discussion of the var keyword in C#.

At first reading of this, it appears to be a simple way of not having to explicitly think about the types of your objects. However, it’s a little more involved than that. It’s useful for when you deal with anonymous types. What’s an anonymous type?

Here’s the link to Microsoft’s discussion of anonymous types.

Anonymous types are class types that derive directly from object, and that cannot be cast to any type exceptobject. The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type.

Here’s some code I created to play around with LINQ in the context of anonymous types . . .

 

        static void Main(string[] args)
        {
            // The Three Parts of a LINQ Query: 
            //  1. Data source. 
            //int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
            var stuff = new[] {new { Number = 0, Fruit = "Apple", Birthday = new DateTime(1968, 5, 31) }, 
                             new { Number = 2, Fruit = "Orange", Birthday = new DateTime(1971, 6, 3) }};

            // 2. Query creation. 
            // stuffQuery is an IEnumerable<?> 
            var stuffQuery =
                from item in stuff
                select new { item.Number };

            // 3. Query execution. 
            foreach (var thing in stuffQuery)
            {
                Console.WriteLine("thing: {0} ", thing.Number);
            }
        }
Posted in C#