<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Ryan Farber's WebLog</title><subtitle type="html" /><id>http://blogs.msdn.com/rfarber/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/rfarber/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2004-10-01T08:40:00Z</updated><entry><title>Reorder Elements with XLinq</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2008/07/22/reorder-elements-with-xlinq.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2008/07/22/reorder-elements-with-xlinq.aspx</id><published>2008-07-22T21:46:55Z</published><updated>2008-07-22T21:46:55Z</updated><content type="html">&lt;p&gt;So recently I was dealing with an internal program that was easily confused and would create XML files with the elements in the wrong order for the schemas used in other internal programs that read these Xml files.&amp;nbsp; I started looking for a fast way to reorder these elements and found this &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3034829&amp;amp;SiteID=1"&gt;forum post&lt;/a&gt; which while on the right track was a bit more complicated than I really needed.&amp;nbsp; So I came up with this solution.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;table cellspacing="0" cellpadding="2" width="400" border="0"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" width="200"&gt;Bad File&lt;/td&gt; &lt;td valign="top" width="200"&gt;Good File&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="200"&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;data&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="ConfigName"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;One&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item One&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;One&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Three&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;A&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item A&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;A&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item C&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item C-2&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;B&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item B&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;B&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Three&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Two&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item Two&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Two&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;data&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/td&gt;
&lt;td valign="top" width="200"&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;data&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="ConfigName"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;One&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item One&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;One&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Two&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item Two&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Two&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Three&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;A&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item A&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;A&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;B&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item B&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;B&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item C&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Item C-2&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;C&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Three&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;data&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Reorder Solution&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt;[] rootOrder = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"One"&lt;/span&gt;, &lt;span class="str"&gt;"Two"&lt;/span&gt;, &lt;span class="str"&gt;"Three"&lt;/span&gt; };
&lt;span class="kwrd"&gt;string&lt;/span&gt;[] threeOrder = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"A"&lt;/span&gt;, &lt;span class="str"&gt;"B"&lt;/span&gt;, &lt;span class="str"&gt;"C"&lt;/span&gt; };
          
XDocument xdoc = XDocument.Load(&lt;span class="str"&gt;"file.xml"&lt;/span&gt;);
Reorder(xdoc, rootOrder);
Reorder(xdoc.Root.Elements(&lt;span class="str"&gt;"Three"&lt;/span&gt;), threeOrder);

&lt;span class="kwrd"&gt;void&lt;/span&gt; Reorder(XDocument xdoc, &lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] order)
{
  xdoc.Root.ReplaceWith(Reorder(xdoc.Root, order));
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; Reorder(IEnumerable&amp;lt;XElement&amp;gt; els, &lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] order)
{
  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (XElement el &lt;span class="kwrd"&gt;in&lt;/span&gt; els.ToArray())
    el.ReplaceWith(Reorder(el, order));
}

XElement Reorder(XElement root, &lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] order)
{
  &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; XElement(root.Name,
                      from el &lt;span class="kwrd"&gt;in&lt;/span&gt; order
                      select root.Elements(el),
                      root.Attributes());
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8764634" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Class Extensions for XLinq</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2008/05/30/class-extensions-for-xlinq.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2008/05/30/class-extensions-for-xlinq.aspx</id><published>2008-05-30T20:57:49Z</published><updated>2008-05-30T20:57:49Z</updated><content type="html">&lt;p&gt;I love XLinq!!&amp;nbsp; It really has made editing and manipulated Xml files much easier for me.&amp;nbsp; However there are some features missing that I really wanted to use all the time so I added them using the new class extensions feature in .NET.&amp;nbsp; So here's what I have.&amp;nbsp; I am looking for a merge nodes functions in XLinq if anyone has any.&lt;/p&gt; &lt;table cellspacing="0" cellpadding="2" width="678" border="1"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" width="203"&gt; &lt;h3&gt;Function&lt;/h3&gt;&lt;/td&gt; &lt;td valign="top" width="473"&gt; &lt;h3&gt;Purpose&lt;/h3&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="206"&gt;IsValid&lt;/td&gt; &lt;td valign="top" width="470"&gt;Validates an XElement against a schemas.&amp;nbsp; I'm really surprised this feature wasn't in XLinq already.&amp;nbsp; I hope its added in the future.&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="209"&gt;GetAttributeValue&lt;/td&gt; &lt;td valign="top" width="468"&gt;There is a SetAttributeValue but no get attribute value.&amp;nbsp; I've overloaded this nicely so it can return defaults and the proper casting.&amp;nbsp; It would be fun to also have it try to look up the declared scema for default values but I haven't had the time or need for that yet.&lt;br&gt;&lt;br&gt;Also I've used the default keyword in this function which I thought was pretty spiffy. :)&lt;br&gt;&lt;br&gt;I most commonly use this for boolean test &lt;br&gt;if (myEl.GetAttributeValue&amp;lt;bool&amp;gt;() == true) { do something }&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="211"&gt;GetElementValue&lt;/td&gt; &lt;td valign="top" width="466"&gt;Ok maybe this function isn't needed that much but I kept finding myself writing the if null and casting code repeatedly plus this nicely matched with GetAttributeValue.&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="213"&gt;RemoveElements&lt;/td&gt; &lt;td valign="top" width="465"&gt;This is another function that really should have existed.&amp;nbsp; They give us Remove, RemoveAll, RemoveAttributes, RemoveNodes, but nothing for removing just the elements and leaving attributes and comments alone.&amp;nbsp; I know its a one liner foreach but it still ticks me off that it wasn't there.&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="214"&gt;HasAttribute&lt;/td&gt; &lt;td valign="top" width="464"&gt;A simple test, does this attribute exist.&amp;nbsp; Certainly a common need.&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="215"&gt;EncodeForXmlAttribute&lt;/td&gt; &lt;td valign="top" width="463"&gt;Ok this one doesn't really belong here but heck I like it being right there at my fingertips to use before setting items or during comparisons.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Xml.Linq;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Xml.Schema;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Xml;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;

&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Class extensions for XElement&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;see cref="IsValid"/&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;see cref="GetAttributeVlue"/&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;see cref="GetElementValue"/&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;see cref="RemoveElements"/&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;see cref="HasAttribute"/&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;see cref="EncodeForXmlAttribute"/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Extensions
{
    &lt;span class="preproc"&gt;#region&lt;/span&gt; XElement &lt;span class="kwrd"&gt;class&lt;/span&gt; extensions
    &lt;span class="preproc"&gt;#region&lt;/span&gt; IsValid (against schemas) Extension &lt;span class="kwrd"&gt;for&lt;/span&gt; XElement
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Validates Xml element against schemas&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Element to verify&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="schemaPath"&amp;gt;List locations of schemas to compare&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;True/False (if false details in Debug)&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsValid(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, &lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] schemaPath)
    {
        XmlSchemaSet schemas = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlSchemaSet();

        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (String sp &lt;span class="kwrd"&gt;in&lt;/span&gt; schemaPath)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (sp.Contains(&lt;span class="str"&gt;"&amp;lt;"&lt;/span&gt;))
            {
                schemas.Add(String.Empty, XElement.Parse(sp).CreateReader());
            }
            &lt;span class="kwrd"&gt;else&lt;/span&gt;
            {
                schemas.Add(String.Empty, XmlReader.Create(sp));
            }
        }

        &lt;span class="kwrd"&gt;return&lt;/span&gt; CompareToSchemas(xEl, schemas);
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Validates xml element against schemas&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Element to verify&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="schemaReader"&amp;gt;List of schemas to compare&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;True/False (if false details in Debug)&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsValid(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, &lt;span class="kwrd"&gt;params&lt;/span&gt; XmlReader[] schemaReader)
    {
        XmlSchemaSet schemas = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlSchemaSet();

        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (XmlReader sr &lt;span class="kwrd"&gt;in&lt;/span&gt; schemaReader)
            schemas.Add(String.Empty, sr);

        &lt;span class="kwrd"&gt;return&lt;/span&gt; CompareToSchemas(xEl, schemas);
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; CompareToSchemas(XElement xEl, XmlSchemaSet schemas)
    {
        &lt;span class="kwrd"&gt;bool&lt;/span&gt; errors = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
        XDocument doc = xEl.Document;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (doc == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            doc = XDocument.Parse(xEl.ToString());
        }

        doc.Validate(schemas, (sender, e) =&amp;gt;
        {
            Debug.WriteLine(e.Message, &lt;span class="str"&gt;"ValidateErrror"&lt;/span&gt;);
            errors = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        });


        &lt;span class="kwrd"&gt;return&lt;/span&gt; !errors;
    }
    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;

    &lt;span class="preproc"&gt;#region&lt;/span&gt; GetAttributeValue
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of an attribute&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="attName"&amp;gt;An XName that contains the name of the attribute to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="defaultReturn"&amp;gt;Default return if the attribute doesn't exist&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Attribute value or default if attribute doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; GetAttributeValue(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName attName, &lt;span class="kwrd"&gt;string&lt;/span&gt; defaultReturn)
    {
        XAttribute att = xEl.Attribute(attName);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (att == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;return&lt;/span&gt; defaultReturn;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; att.Value;
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of an attribute&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="attName"&amp;gt;An XName that contains the name of the attribute to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Attribute value or String.Empty if element doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; GetAttributeValue(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName attName)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; xEl.GetAttributeValue(attName, String.Empty);
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of an attribute&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="attName"&amp;gt;An XName that contains the name of the attribute to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="defaultReturn"&amp;gt;Default return if the attribute doesn't exist&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Attribute value or default if attribute doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetAttributeValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName attName, T defaultReturn)
    {
        &lt;span class="kwrd"&gt;string&lt;/span&gt; returnValue = xEl.GetAttributeValue(attName, String.Empty);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (returnValue == String.Empty) &lt;span class="kwrd"&gt;return&lt;/span&gt; defaultReturn;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (T)Convert.ChangeType(returnValue, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T));
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of an attribute&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="attName"&amp;gt;An XName that contains the name of the attribute to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Attribute value or default of T if element doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetAttributeValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName attName)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; xEl.GetAttributeValue&amp;lt;T&amp;gt;(attName, &lt;span class="kwrd"&gt;default&lt;/span&gt;(T));
    }
    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;

    &lt;span class="preproc"&gt;#region&lt;/span&gt; GetElementValue
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of a child element.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="elName"&amp;gt;An XName that contains the name of the child element to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="defaultReturn"&amp;gt;Default return if the element doesn't exist&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Element value or default if element doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; GetElementValue(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName elName, &lt;span class="kwrd"&gt;string&lt;/span&gt; defaultReturn)
    {
        XElement el = xEl.Element(elName);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (el == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;return&lt;/span&gt; defaultReturn;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; el.Value;
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of a child element.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="elName"&amp;gt;An XName that contains the name of the child element to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Element value or String.Empty if element doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; GetElementValue(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName elName)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; xEl.GetElementValue(elName, String.Empty);
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of a child element.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="elName"&amp;gt;An XName that contains the name of the child element to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="defaultReturn"&amp;gt;Default return if the element doesn't exist&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Element value or default if element doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetElementValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName elName, T defaultReturn)
    {
        &lt;span class="kwrd"&gt;string&lt;/span&gt; returnValue = xEl.GetElementValue(elName, String.Empty);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (returnValue == String.Empty) &lt;span class="kwrd"&gt;return&lt;/span&gt; defaultReturn;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (T)Convert.ChangeType(returnValue, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T));
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the value of a child element.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="elName"&amp;gt;An XName that contains the name of the child element to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Element value or default of T if element doesn't exist&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetElementValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName elName)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; xEl.GetElementValue&amp;lt;T&amp;gt;(elName, &lt;span class="kwrd"&gt;default&lt;/span&gt;(T));
    }
    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;

    &lt;span class="preproc"&gt;#region&lt;/span&gt; RemoveElements
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Removes all child elements&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RemoveElements(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl)
    {
       &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (XElement el &lt;span class="kwrd"&gt;in&lt;/span&gt; xEl.Elements().ToArray()) el.Remove();
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Removes all child elements matching the XName&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="name"&amp;gt;The System.Xml.Linq.XName to match.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RemoveElements(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName name)
    {
        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (XElement el &lt;span class="kwrd"&gt;in&lt;/span&gt; xEl.Elements(name).ToArray()) el.Remove();
    }
    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Determines if attribute doesn't exist or exists but is empty&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="xEl"&amp;gt;Extends this XElement Type&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="attName"&amp;gt;An XName that contains the name of the attribute to retrieve.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;True if attribute exists and is not empty&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; HasAttribute(&lt;span class="kwrd"&gt;this&lt;/span&gt; XElement xEl, XName attName)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; !String.IsNullOrEmpty(xEl.GetAttributeValue(attName));
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Replaces invalid XML characters in a string with their valid XML equivalent.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;remarks&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Replaced characters are &amp;amp;lt; to &amp;amp;amp;lt;, &amp;amp;gt; to &amp;amp;amp;gt;, &amp;amp;apos; to &amp;amp;amp;apos;, &amp;amp;quot; to &amp;amp;amp;quot; and &amp;amp;amp; to &amp;amp;amp;amp;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/remarks&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="text"&amp;gt;The string within which to escape invalid characters.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;The input string with invalid characters replaced.&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; EncodeForXmlAttribute(String text)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; System.Security.SecurityElement.Escape(text);
    }
    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8563831" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Find and FindAll in .NET 2.0 collections</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2006/07/28/681578.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2006/07/28/681578.aspx</id><published>2006-07-28T18:25:00Z</published><updated>2006-07-28T18:25:00Z</updated><content type="html">&lt;FONT size=2&gt;
&lt;P&gt;I just found that Arrays, Lists, Collections all have these Find, FindAll, etc... functions that use the Predicate syntax.&amp;nbsp; I guess I've noticed them a few times but never actually tried to use them.&amp;nbsp; Check this out.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff size=2&gt;static&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; Main(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;string&lt;/FONT&gt;&lt;FONT size=2&gt;[] args)&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// List of Integers&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;[] intArray = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;[] { 1, 5, 10, 2, 15, 100, 20, 30 };&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;[] intFilter;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Get all the integers that are less than five [using inline predicate delage]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT size=2&gt;intFilter = &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Array&lt;/FONT&gt;&lt;FONT size=2&gt;.FindAll&amp;lt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;gt;(intArray, &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;delegate&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; testInt) { &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; testInt &amp;lt; 5; });&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DisplayIntList(intFilter);&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// If I reused it a lot I can turn it into a function&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT size=2&gt;intFilter = &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Array&lt;/FONT&gt;&lt;FONT size=2&gt;.FindAll&amp;lt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;gt;(intArray, LessThanTen);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DisplayIntList(intFilter);&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// If I wanted to pass in the limit I'd have to create a Predicate&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT size=2&gt;intFilter = &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Array&lt;/FONT&gt;&lt;FONT size=2&gt;.FindAll&amp;lt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;gt;(intArray, LessThan(50));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;DisplayIntList(intFilter);&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;static&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;bool&lt;/FONT&gt;&lt;FONT size=2&gt; LessThanTen(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; testInt)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; testInt &amp;lt; 10; &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;static&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Predicate&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;gt; LessThan(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; limit)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;delegate&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; testInt) { &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; testInt &amp;lt; limit; };&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;static&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; DisplayIntList(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;[] intFilter)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;foreach&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; testInt &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;in&lt;/FONT&gt;&lt;FONT size=2&gt; intFilter) { &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Console&lt;/FONT&gt;&lt;FONT size=2&gt;.WriteLine(testInt); } &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Console&lt;/FONT&gt;&lt;FONT size=2&gt;.WriteLine();&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=681578" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>High Contrast Mode</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2005/10/21/483607.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2005/10/21/483607.aspx</id><published>2005-10-22T00:32:00Z</published><updated>2005-10-22T00:32:00Z</updated><content type="html">&lt;P&gt;We are working on a simple helper tool for testing accessibility in IE pages.&amp;nbsp; One of the few tests that is pretty hard to automate since it has to be reviewed first, although once reviewed screen shot comparisons of regressions could be useful assuming the pages are reasonably static.&amp;nbsp; Either way this tool is a simple set of options for IE.&amp;nbsp; Turn on/off IE settings for Images, Colors, and CSS; resize IE window for resolution testing (ok not the same thing but reasonably the same effect), Alt text expand, and font size.&amp;nbsp; The tool sets the IE registry settings then opens IE on the test url.&amp;nbsp; After you are done it resets all the registry setting.&amp;nbsp; Nice, simple, and easy to hand to a dev for unit tests.&amp;nbsp; The only feature we wanted to add the was somewhat difficult was setting Windows into High Contrast Mode.&amp;nbsp; After long searching for how to change high contrast mode in .NET I came up with the following.&amp;nbsp; &lt;BR&gt;&lt;BR&gt;&lt;FONT size=1&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Structure&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; tagHIGHCONTRAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Dim&lt;/FONT&gt;&lt;FONT size=1&gt; cbSize &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Dim&lt;/FONT&gt;&lt;FONT size=1&gt; dwFlags &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Dim&lt;/FONT&gt;&lt;FONT size=1&gt; lpszDefaultScheme &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;End&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Structure&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Const&lt;/FONT&gt;&lt;FONT size=1&gt; HCF_HIGHCONTRASTON &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; = &amp;amp;H1&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Const&lt;/FONT&gt;&lt;FONT size=1&gt; SPI_GETHIGHCONTRAST &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; = 66&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Const&lt;/FONT&gt;&lt;FONT size=1&gt; SPI_SETHIGHCONTRAST &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; = 67&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Const&lt;/FONT&gt;&lt;FONT size=1&gt; SPIF_UPDATEINIFILE &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; = &amp;amp;H1&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Const&lt;/FONT&gt;&lt;FONT size=1&gt; SPIF_SENDWININICHANGE &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; = &amp;amp;H2&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Public&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Const&lt;/FONT&gt;&lt;FONT size=1&gt; SPIF_SENDCHANGE &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; = SPIF_SENDWININICHANGE&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Declare&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Function&lt;/FONT&gt;&lt;FONT size=1&gt; SystemParametersInfo &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Lib&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=1&gt;"user32"&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Alias&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=1&gt;"SystemParametersInfoA"&lt;/FONT&gt;&lt;FONT size=1&gt; ( _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;ByVal&lt;/FONT&gt;&lt;FONT size=1&gt; uAction &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;FONT size=1&gt;, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;ByVal&lt;/FONT&gt;&lt;FONT size=1&gt; uParam &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;FONT size=1&gt;, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;ByRef&lt;/FONT&gt;&lt;FONT size=1&gt; lpvParam &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; tagHIGHCONTRAST,&amp;nbsp;_&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ByVal&lt;/FONT&gt;&lt;FONT size=1&gt; fuWinIni &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/FONT&gt;&lt;FONT size=1&gt;) &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Integer&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Private&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Sub&lt;/FONT&gt;&lt;FONT size=1&gt; SetHighContrast(&lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;ByVal&lt;/FONT&gt;&lt;FONT size=1&gt; TurnOn &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Boolean&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Dim&lt;/FONT&gt;&lt;FONT size=1&gt; udtHighContrast &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; tagHIGHCONTRAST&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;Dim&lt;/FONT&gt;&lt;FONT size=1&gt; lngWin32apiResultCode &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;As&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Long&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt;udtHighContrast.cbSize = Len(udtHighContrast)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;lngWin32apiResultCode = SystemParametersInfo(SPI_GETHIGHCONTRAST, Len(udtHighContrast), udtHighContrast, 0)&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;If&lt;/FONT&gt;&lt;FONT size=1&gt; TurnOn &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt;udtHighContrast.dwFlags = udtHighContrast.dwFlags &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=1&gt;Or&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; HCF_HIGHCONTRASTON&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt;udtHighContrast.dwFlags = udtHighContrast.dwFlags &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=1&gt;Xor&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; HCF_HIGHCONTRASTON&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;End&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;If&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt;lngWin32apiResultCode = SystemParametersInfo(SPI_SETHIGHCONTRAST, Len(udtHighContrast), udtHighContrast, SPIF_UPDATEINIFILE &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=1&gt;Or&lt;/FONT&gt;&lt;FONT size=1&gt;&lt;FONT face="Courier New"&gt; SPIF_SENDCHANGE)&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=1&gt;End&lt;/FONT&gt;&lt;FONT size=1&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=1&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=483607" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Teaching non-enthusiastic students</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2005/06/22/431615.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2005/06/22/431615.aspx</id><published>2005-06-22T20:15:00Z</published><updated>2005-06-22T20:15:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Verdana&gt;I work with several really good black box testers who know that it is time to increase their skills and learn to program.&amp;nbsp; These skills hopefully will add them to list of people who can fix test automations, create simple automations and better understand potential weaknesess in developers code.&amp;nbsp; All in all better testers.&amp;nbsp; The problem is that even though their review goals emphasize increasing technical skills and they even take classes on Intro to C# or Programming Principles or even weekly brown-bag programming talks these training sessions never turn into actual productive skills.&lt;BR&gt;&lt;BR&gt;Then one day I notice that one of these testers has found an API for automating dialogs through sending key strokes to window handles.&amp;nbsp; He went hog wild.&amp;nbsp; The code wasn't great and completely unsustainable but there he was building projects in C#.&amp;nbsp; So I sat down with another trainer and started discussing this.&amp;nbsp; We looked at what kind of programs we would like these testers to write and built and API that allowed them to more easily run these tasks.&amp;nbsp; Specifically for our needs we wrapped functions needed for writing batch programs (better File Copy/Move, Modify XML file settings, Simple search and replace of values in text files, abilities to determine file encoding) and IE UI automation thourgh the IE DOM.&amp;nbsp;&amp;nbsp; Then built a custom template for BatchCode and IEUICode that created the projects, added needed references, and supplied sample code.&lt;BR&gt;&lt;BR&gt;Wow did this work.&amp;nbsp; At first they started converting some of their batch programs, then writing test cases.&amp;nbsp; If a tricky problem came up that was reusable we would add a function to the API.&amp;nbsp; In less than a month we had adoption by about a dozen people who were debugging, writing functions and discussing code.&amp;nbsp; Adding FAQ documents and modifying the templates to push toward sustainable programming techniques helped as well.&lt;BR&gt;&lt;BR&gt;In summary we&amp;nbsp;developed an easy to use API that very directly affected our&amp;nbsp;business.&amp;nbsp; This API allowed people new to programming to produce useful results quickly.&amp;nbsp; These fast wins turn into excitement which in turn increases technical skills much more efficiently than any training courses we developed or sent people to.&amp;nbsp; From there training becomes more effective.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=431615" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Capturing web page images</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2005/06/22/431599.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2005/06/22/431599.aspx</id><published>2005-06-22T19:47:00Z</published><updated>2005-06-22T19:47:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Verdana&gt;After fiddling with DrawToDC I discovered it won't capture DXFilters since they aren't technically in the DC.&amp;nbsp; Not to mention that getting the copied image in a state for actually saving to a file is more pain that I wanted to go to.&amp;nbsp; I decided the best way to handle this was to deal with the limitation that it won't work on Locked Machine or in Service Instances (though with screensavers is fine) and I've built a form with a AxBrowser control on it. and used this code for capturing.&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;private Bitmap m_CapturedImage&amp;nbsp;= null;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;/// &amp;lt;summary&amp;gt;&lt;BR&gt;/// Captures the current web page&lt;BR&gt;/// &amp;lt;/summary&amp;gt;&lt;BR&gt;/// &amp;lt;overloads&amp;gt;&lt;BR&gt;/// &amp;lt;summary&amp;gt;&lt;BR&gt;/// Captures the current web page&lt;BR&gt;/// &amp;lt;/summary&amp;gt;&lt;BR&gt;/// &amp;lt;example&amp;gt;&lt;BR&gt;/// Capture the full image of a page (scrolls down the page) and returns an image&lt;BR&gt;/// &amp;lt;code&amp;gt;&lt;BR&gt;/// Image pageCapture = browser.CapturePage();&lt;BR&gt;/// pageCapture.Save("C:\\test.bmp", ImageFormat.Bmp);&lt;BR&gt;/// &amp;lt;/code&amp;gt;&lt;BR&gt;/// &amp;lt;/example&amp;gt;&lt;BR&gt;/// &amp;lt;example&amp;gt;&lt;BR&gt;/// Capture top section of a page&lt;BR&gt;/// &amp;lt;code&amp;gt;&lt;BR&gt;/// Image pageCapture = browser.CapturePage(new Rectangle(0,0, 50, 50), false);&lt;BR&gt;/// pageCapture.Save("C:\\test.bmp", ImageFormat.Bmp);&lt;BR&gt;/// &amp;lt;/code&amp;gt;&lt;BR&gt;/// &amp;lt;/example&amp;gt;&lt;BR&gt;/// &amp;lt;/overloads&amp;gt;&lt;BR&gt;/// &amp;lt;example&amp;gt;&lt;BR&gt;/// Capture the full image of a page (scrolls down the page) and save to the harddrive.&lt;BR&gt;/// &amp;lt;code&amp;gt;&lt;BR&gt;/// Image pageCapture = browser.CapturePage();&lt;BR&gt;/// pageCapture.Save("C:\\test.bmp", ImageFormat.Bmp);&lt;BR&gt;/// &amp;lt;/code&amp;gt;&lt;BR&gt;/// &amp;lt;/example&amp;gt;&lt;BR&gt;/// &amp;lt;returns&amp;gt;Image object of the requested capture&amp;lt;/returns&amp;gt;&lt;BR&gt;&lt;/FONT&gt;public Image CapturePage() { return CapturePage(new Rectangle(0,0,0,0), true); }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;/// &amp;lt;summary&amp;gt;&lt;BR&gt;/// Captures the current web page&lt;BR&gt;/// &amp;lt;/summary&amp;gt;&lt;BR&gt;/// &amp;lt;example&amp;gt;&lt;BR&gt;/// Capture top section of a page&lt;BR&gt;/// &amp;lt;code&amp;gt;&lt;BR&gt;/// Image pageCapture = browser.CapturePage(new Rectangle(0,0, 50, 50), false);&lt;BR&gt;/// pageCapture.Save("C:\\test.bmp", ImageFormat.Bmp);&lt;BR&gt;/// &amp;lt;/code&amp;gt;&lt;BR&gt;/// &amp;lt;/example&amp;gt;&lt;BR&gt;/// &amp;lt;param name="captureRegion"&amp;gt;Coordinates to capture on the page.&amp;lt;/param&amp;gt;&lt;BR&gt;/// &amp;lt;param name="scrollDownPage"&amp;gt;Scroll down the page when capturing &lt;BR&gt;///&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (this is better disabled on framesets)&amp;lt;/param&amp;gt;&lt;BR&gt;/// &amp;lt;returns&amp;gt;Image object of the requested capture&amp;lt;/returns&amp;gt;&lt;BR&gt;&lt;/FONT&gt;public Image CapturePage(Rectangle captureRegion, bool scrollDownPage)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;if(this.ParentForm != null)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;if(this.ParentForm.Opacity &amp;lt; 1.0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.ParentForm.Top = SystemInformation.VirtualScreen.Top - this.ParentForm.Height - 100;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.ParentForm.Left = SystemInformation.VirtualScreen.Left - this.ParentForm.Width - 100;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.ParentForm.Opacity = 1.0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;VScrollBarOffset&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;HScrollBarOffset&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;PixelsScrolled&amp;nbsp;&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;CaptureHeight&amp;nbsp;&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;CaptureWidth&amp;nbsp;&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;FinalPageHeight&amp;nbsp;&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;CaptureDestinationY&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;int&amp;nbsp;&amp;nbsp;&amp;nbsp;CaptureDestinationX&amp;nbsp;&amp;nbsp;= 0;&lt;BR&gt;&amp;nbsp;Rectangle&amp;nbsp;CaptureRectangle;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;// Captured page specific properties.&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;bool m_ScrollV&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= true;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;// Get fresh copy of the doc objects (they need to be updated whenever the Navigation changes)&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;HTMLDocumentClass m_HTMLDoc&amp;nbsp;= (HTMLDocumentClass)this.Document;&lt;BR&gt;&amp;nbsp;HTMLBodyClass m_HTMLBody&amp;nbsp;= (HTMLBodyClass)&amp;nbsp;&amp;nbsp;m_HTMLDoc.body;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;#region Figure Max Height and Set destination bitmap&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;// Check if there is a Horizontal Scroll bar&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;if(m_HTMLBody.scrollWidth &amp;gt; m_HTMLBody.clientWidth)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;HScrollBarOffset = SystemInformation.HorizontalScrollBarHeight;&lt;BR&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;// Get final page height and height of capture area&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;CaptureHeight&amp;nbsp;= browser.Height - &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SystemInformation.Border3DSize.Height - &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HScrollBarOffset;&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&amp;nbsp;if(m_ScrollV) // || m_HTMLBody.scrollHeight &amp;lt; m_PageHeight)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;FinalPageHeight = m_HTMLBody.scrollHeight;&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;else&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;FinalPageHeight = CaptureHeight;&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;#endregion&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;#region Figure Width minus Vertical Scroll bar&lt;BR&gt;&amp;nbsp;VScrollBarOffset = SystemInformation.Border3DSize.Width;&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;// Set offset (0 if scroll bar is on right, VSBarWidth if on left (RTL)&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;if((m_HTMLDoc.dir != null &amp;amp;&amp;amp; m_HTMLDoc.dir.ToLower() == "rtl") || &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (m_HTMLBody.dir != null &amp;amp;&amp;amp; m_HTMLBody.dir.ToLower() == "rtl"))&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;VScrollBarOffset += SystemInformation.VerticalScrollBarWidth;&lt;BR&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;CaptureWidth = browser.Width - &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;SystemInformation.VerticalScrollBarWidth - &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SystemInformation.Border3DSize.Width - &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SystemInformation.Border3DSize.Width;&lt;BR&gt;&amp;nbsp;#endregion&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;#region Capture Full Web Page&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;// Create a Bitmap of the final size&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;m_CapturedImage = new Bitmap(CaptureWidth, FinalPageHeight);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;PixelsScrolled = CaptureHeight - SystemInformation.Border3DSize.Height;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;m_HTMLBody.scrollTop = 0;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;// Build a full picture from each section of the page.&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;if(!m_ScrollV)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;CaptureRectangle = new Rectangle(VScrollBarOffset, CaptureHeight - PixelsScrolled, CaptureWidth, CaptureHeight);&lt;BR&gt;&amp;nbsp;&amp;nbsp;AddToScreenShot(CaptureRectangle, CaptureDestinationX, CaptureDestinationY);&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;else&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;int MaxScrolls = 0;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;if(scrollDownPage)&lt;BR&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;MaxScrolls = m_HTMLBody.scrollHeight / CaptureHeight;&lt;BR&gt;&amp;nbsp;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;do&lt;BR&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;CaptureRectangle = new Rectangle(VScrollBarOffset, CaptureHeight - PixelsScrolled, CaptureWidth, PixelsScrolled);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;AddToScreenShot(CaptureRectangle, CaptureDestinationX, CaptureDestinationY);&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;CaptureDestinationY = CaptureDestinationY + PixelsScrolled;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;PixelsScrolled = scrollDown(HScrollBarOffset);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;MaxScrolls--;&lt;BR&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;while(MaxScrolls &amp;gt; -1);&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;#endregion&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;if(captureRegion.Width != 0 &amp;amp;&amp;amp; captureRegion.Height != 0)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;return m_CapturedImage.Clone(captureRegion, m_CapturedImage.PixelFormat);&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;return m_CapturedImage;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;/// &amp;lt;summary&amp;gt;&lt;BR&gt;/// Scroll the page down one page.&lt;BR&gt;/// &amp;lt;/summary&amp;gt;&lt;BR&gt;/// &amp;lt;returns&amp;gt;Number of pixels scrolled&amp;lt;/returns&amp;gt;&lt;BR&gt;&lt;/FONT&gt;private int scrollDown(int HScrollBarOffset)&lt;BR&gt;{&amp;nbsp;&lt;BR&gt;&amp;nbsp;HTMLDocumentClass m_HTMLDoc&amp;nbsp;= (HTMLDocumentClass)this.Document;&lt;BR&gt;&amp;nbsp;HTMLBodyClass m_HTMLBody&amp;nbsp;= (HTMLBodyClass)&amp;nbsp;&amp;nbsp;m_HTMLDoc.body;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;int CaptureHeight&amp;nbsp;&amp;nbsp;= m_HTMLBody.offsetHeight;&lt;BR&gt;&amp;nbsp;int ScrollTop&amp;nbsp;&amp;nbsp;&amp;nbsp;= m_HTMLBody.scrollTop;&lt;BR&gt;&amp;nbsp;int scrollAmount&amp;nbsp;&amp;nbsp;= ScrollTop + CaptureHeight - (m_HTMLBody.clientTop * 2) - HScrollBarOffset;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;m_HTMLBody.scrollTop = scrollAmount;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;int ScrollTopAfterScroll = 0;&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&amp;nbsp;if(m_HTMLBody.scrollTop == 0 &amp;amp;&amp;amp; m_HTMLBody.scrollHeight &amp;gt;= scrollAmount)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;Object parent = m_HTMLDoc.documentElement;&lt;BR&gt;&amp;nbsp;&amp;nbsp;PropertyInfo prop = parent.GetType().GetProperty("scrollTop");&lt;BR&gt;&amp;nbsp;&amp;nbsp;prop.SetValue(parent, scrollAmount, null);&lt;BR&gt;&amp;nbsp;&amp;nbsp;ScrollTopAfterScroll = (int)prop.GetValue(parent, null);&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;else&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;ScrollTopAfterScroll = m_HTMLBody.scrollTop;&lt;BR&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;int PixelsScrolled = ScrollTopAfterScroll - ScrollTop;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;if(PixelsScrolled &amp;lt;= 0)&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;return 0;&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;else&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;return PixelsScrolled;&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;/// &amp;lt;summary&amp;gt;&lt;BR&gt;/// Copies visible image to a destination bitmap&lt;BR&gt;/// &amp;lt;/summary&amp;gt;&lt;BR&gt;/// &amp;lt;param name="CaptureRectangle"&amp;gt;Size within browser control to capture&amp;lt;/param&amp;gt;&lt;BR&gt;/// &amp;lt;param name="CaptureDestinationY"&amp;gt;Starting Y postion for image in destination bitmap&amp;lt;/param&amp;gt;&lt;BR&gt;/// &amp;lt;param name="CaptureDestinationX"&amp;gt;Starting X postion for image in destination bitmap&amp;lt;/param&amp;gt;&lt;BR&gt;&lt;/FONT&gt;private void AddToScreenShot(Rectangle CaptureRectangle, int CaptureDestinationX, int CaptureDestinationY)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;if(this.ParentForm != null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;this.ParentForm.TransparencyKey = this.ParentForm.TransparencyKey; &lt;FONT color=#008000&gt;// Without this images off main montior won't display&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;Application.DoEvents(); &lt;FONT color=#008000&gt;// Makes sure the screen redraws&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;Graphics&amp;nbsp;g1&amp;nbsp;&amp;nbsp;&amp;nbsp;= null;&lt;BR&gt;&amp;nbsp;Graphics&amp;nbsp;g2&amp;nbsp;&amp;nbsp;&amp;nbsp;= null;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;try&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;// Get Graphic object for source and destination&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;g1&amp;nbsp;&amp;nbsp;&amp;nbsp;= browser.CreateGraphics();&lt;BR&gt;&amp;nbsp;&amp;nbsp;g2&amp;nbsp;&amp;nbsp;&amp;nbsp;= Graphics.FromImage(m_CapturedImage);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;IntPtr&amp;nbsp;&amp;nbsp;dc1&amp;nbsp;&amp;nbsp;&amp;nbsp;= IntPtr.Zero;&lt;BR&gt;&amp;nbsp;&amp;nbsp;IntPtr&amp;nbsp;&amp;nbsp;dc2&amp;nbsp;&amp;nbsp;&amp;nbsp;= IntPtr.Zero;&lt;BR&gt;&amp;nbsp;&amp;nbsp;try&lt;BR&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Get the handles for the Graphics which are needed to perform the image copy&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;dc1&amp;nbsp;&amp;nbsp;&amp;nbsp;= g1.GetHdc();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;dc2&amp;nbsp;&amp;nbsp;&amp;nbsp;= g2.GetHdc();&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Create a destination image the size of the visible browser window&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;BitBlt(dc2, CaptureDestinationX, CaptureDestinationY, CaptureRectangle.Width, CaptureRectangle.Height,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dc1, CaptureRectangle.X , CaptureRectangle.Y , 13369376);&lt;BR&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;finally&lt;BR&gt;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Release holds on handles&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(dc1 != IntPtr.Zero) g1.ReleaseHdc(dc1);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(dc2 != IntPtr.Zero) g2.ReleaseHdc(dc2);&lt;BR&gt;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;finally&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;// Release holds on graphics&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;if(g1 != null) g1.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;if(g2 != null) g2.Dispose();&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008000&gt;/// &amp;lt;summary&amp;gt;&lt;BR&gt;/// BitBlit it a core GDI call which can copy an image from one hdc to another&lt;BR&gt;/// &amp;lt;/summary&amp;gt;&lt;BR&gt;&lt;/FONT&gt;[DllImport("GDI32.DLL", EntryPoint="BitBlt", SetLastError=true,&lt;BR&gt;&amp;nbsp;&amp;nbsp;CharSet=CharSet.Unicode, ExactSpelling=true,&lt;BR&gt;&amp;nbsp;&amp;nbsp;CallingConvention=CallingConvention.StdCall)]&lt;BR&gt;static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest,&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;int nWidth, int nHeight, IntPtr hdcSrc, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int nXSrc, int nYSrc, System.Int32 dwRop);&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=431599" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>DrawToDC</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/12/240943.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/12/240943.aspx</id><published>2004-10-12T16:06:00Z</published><updated>2004-10-12T16:06:00Z</updated><content type="html">&lt;p class="MsoNormal"&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;I’m trying to take this code &lt;a title="http" href="http://www.codeproject.com/internet/htmlimagecapture.asp?msg=783507"&gt;http://www.codeproject.com/internet/htmlimagecapture.asp?msg=783507&lt;/a&gt; and convert it to C#.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal"&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;I can’t seem cast from IHTMLElement down to IHTMLElementRender.&amp;nbsp; Trying through late binding also fails.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal"&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;IHTMLDocument2 doc = (IHTMLDocument2)Browser.Document;&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;IHTMLElement body = (IHTMLElement)doc.body;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal"&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;// Render returns as null&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;IHTMLElementRender render = (IHTMLElementRender)body;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal"&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;// Render is also null&lt;br /&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;IHTMLElementRender render = body.getType().GetInterface(“IHTMLElementRender”);&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal"&gt;&lt;font face="Arial" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Does anyone have working C# calling DrawToDC?&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=240943" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Screen capture when machine is locked?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/07/239067.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/07/239067.aspx</id><published>2004-10-07T22:45:00Z</published><updated>2004-10-07T22:45:00Z</updated><content type="html">&lt;font face="Arial"&gt;Does anybody out there know how to get a screen capture (specifically of the WebControls web browser) when the screen is locked.&amp;nbsp; When I move the window to the top left outside the monitor bounds it captures with a screen saver on but not when the machine is locked.&amp;nbsp; Any ideas?&lt;/font&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=239067" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Fable</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/07/238941.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/07/238941.aspx</id><published>2004-10-07T17:56:00Z</published><updated>2004-10-07T17:56:00Z</updated><content type="html">&lt;p&gt;&lt;font face="Arial"&gt;If you haven't picked up a copy yet go and get Fable for the XBOX.&amp;nbsp; This is a fun game.&amp;nbsp; I think its the first adventure game I've played all the way through since Oddworld.&amp;nbsp; It successfully put together game play fluidly with the story movies much like Prince of Persia but without the boredom of repetitive moves and simple puzzles.&amp;nbsp; You can customize your character is so many ways it makes you want to go play the game again with a new outlook after completing it.&amp;nbsp; Very nice!&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;Of course now I need to get my hands on Halo 2...November 9th....arrggg!!!&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=238941" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Speaking of Uri’s</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/06/238177.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/06/238177.aspx</id><published>2004-10-06T13:30:00Z</published><updated>2004-10-06T13:30:00Z</updated><content type="html">&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Changing my code to convert string Url’s to Uri objects (because FxCop said it was a good idea) really helped solidify my testing.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I never really thought about picking up Url’s that may not be http.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I can see where a user may have done this with my code.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Now I catch it upfront.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;table class="MsoTableGrid" style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #d9d9d9; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellspacing="0" cellpadding="0" border="1"&gt; &lt;tbody&gt; &lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes"&gt; &lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 6.15in; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt" valign="top" width="590"&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;void DoSomething(string url)&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;// Will throw ArgumentNull or UriFormatException if Url is not valid&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;Uri myUrl = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; Uri(url); &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;if&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;(myUrl.Scheme != Uri.UriSchemeHttp&amp;nbsp;&amp;amp;&amp;amp; myUrl.Scheme != Uri.UriSchemeHttps)&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; ArgumentException("Expected HTTP or HTTPS url",url);&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// Do Something&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Use of included script samples are subject to the terms specified at &lt;a href="http://www.microsoft.com/info/cpyright.htm"&gt;http://www.microsoft.com/info/cpyright.htm&lt;/a&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=238177" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>FxCop is your friend</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/05/237573.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/05/237573.aspx</id><published>2004-10-05T11:02:00Z</published><updated>2004-10-05T11:02:00Z</updated><content type="html">&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If you haven’t used FxCop and you are writing code in C# go get it at &lt;a href="http://www.gotdotnet.com/team/fxcop/"&gt;http://www.gotdotnet.com/team/fxcop/&lt;/a&gt; .&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This program rocks and will make your code better.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The hard part is that on first run you’re going to get a surprising amount of issues and start trying to ignore half of the bugs.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I mean why program for global concerns if I know the code is always going to in English.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Because it’s the right thing to do!&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You never know what you are going to reuse so take the time to write it correctly.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;For example I was pretty bugged at the issue raised that any parameter containing the word “Url” should be turned into an Uri object or at least only be an overload that turns it into an Uri object to pass to the real code.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I don’t need that.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Well it bugged me enough that instead of excluding the error I tried changing my code.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It really does make my code better.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Thanks FxCop.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=237573" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Two simple file tricks</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/03/237000.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/03/237000.aspx</id><published>2004-10-03T11:17:00Z</published><updated>2004-10-03T11:17:00Z</updated><content type="html">&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;1. How do I know if a file path string has valid characters without resorting to catching an exception?&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;table class="MsoTableGrid" style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #d9d9d9; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellspacing="0" cellpadding="0" border="1"&gt; &lt;tbody&gt; &lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes"&gt; &lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 6.15in; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt" valign="top" width="590"&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;bool IsFileNameValid(string fileName)&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;{&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;if&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;(filename != null &amp;amp;&amp;amp; fileName.IndexOfAny(Path.InvalidPathChars) != -1)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;{ return false; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;else { return true; }&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;2. Ever wanted to get the parent folder name of file path?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;table class="MsoTableGrid" style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #d9d9d9; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellspacing="0" cellpadding="0" border="1"&gt; &lt;tbody&gt; &lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes"&gt; &lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 6.15in; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt" valign="top" width="590"&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;Path.GetFileName(Path.GetDirectoryName(path))&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;The GetDirectoryName strips off the file name and then the GetFileName returns the directory thinking it’s the file name.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This of course assumes you know the path really ends with a file name.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Use of included script samples are subject to the terms specified at &lt;a href="http://www.microsoft.com/info/cpyright.htm"&gt;http://www.microsoft.com/info/cpyright.htm&lt;/a&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=237000" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>Teaching C# to newbie testers</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/02/236672.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/02/236672.aspx</id><published>2004-10-02T12:54:00Z</published><updated>2004-10-02T12:54:00Z</updated><content type="html">&lt;P class=MsoNormal&gt;&lt;SPAN&gt;I keep looking for simple books for programming C# but even the learn in 21 days books are trying to teach too much.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I’d hazard to say 99% of any code needed to be written by testers is simple file manipulation.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;You need to know about int, string, building console apps, finding files, opening and saving files, loops, and simple string replacement and verification.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;That’s about it.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I’m working on a paper to teach that basic things right now.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I think it might be useful.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Guess I’ll find out.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=236672" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry><entry><title>I love C#</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/rfarber/archive/2004/10/01/236325.aspx" /><id>http://blogs.msdn.com/rfarber/archive/2004/10/01/236325.aspx</id><published>2004-10-01T15:40:00Z</published><updated>2004-10-01T15:40:00Z</updated><content type="html">&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;I love C#.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It’s everything VB/VBS promised but didn’t quite give us. I was a hard core VB junky following the maxim “get it done today!”&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;VB allowed me to do that.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;OK so every once in a while I needed code in C++ to keep my stuff reasonably speedy but 99% of the I could complete a functional program with no worries of memory leaks in a quarter of the time it took my C++ buddies to do.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Of course they laughed at me during code reviews but heck I was productive.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Now with C# the laughing has ended.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;I’m in the club now and still quickly getting things done.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=236325" width="1" height="1"&gt;</content><author><name>rfarber</name><uri>http://blogs.msdn.com/members/rfarber.aspx</uri></author></entry></feed>