Thursday, July 31, 2008

XML Fluent Interface

Mark S. Rasmussen recently posted a wonderful way of writing out XML text in .Net simply, I found it amazingly useful for Flash development to take data and make XML files for consumption of the swf file. Several of the commentor's mentioned some improvements that could be made to the class, so I took his class and modified it.

The changes were sometime superficial, like I renamed some of the properties to names that made a little more sense (i.e., within renamed to ParentOf). Where as some of the changes were more to the core of the class, like where he used XmlDocument to write the XML, I used XmlTextWriter which made node stack management much easier. I agreed with his thought though that sometimes you don't want raw XML but you want to manipulate it still. So I added properties to output to string, XmlDocument, or XmlTextWriter (though why you'd use the last one I am not sure).

I redid his more complex structure example for you to see below:
---

xOut xo = new xOut()
.XmlDeclaration()
.Node( "root" ).ParentOf()
.Node("user").ParentOf()
.Node("username").InnerText("orca")
.Node("realname").InnerText("Mark S. Rasmussen")
.Node("description").InnerText("I'll handle any escaping (like < & > for example) needs automagically.")
.Node("articles").ParentOf()
.Node("article").Attribute("id", "25").InnerText("Handling DBNulls")
.Node("article").Attribute("id", "26").InnerText("Accessing my privates")
.CloseParent()
.Node("hobbies").ParentOf()
.Node("hobby").InnerText("Fishing")
.Node("hobby").InnerText("Photography")
.Node("hobby").InnerText("Work");


As you can see, it works very similarly to his class. It's not perfect, but ladies and gentlemen here's my class xOut:
---

using System;
using System.Xml;
using System.IO;

public class xOut
{
// The internal XmlWriter that writes the XML.
private XmlTextWriter xw;

//The internal memory stream that holds the XML.
private MemoryStream ms;

// Whether the next node should be created in the scope of the current node.
bool nextNodeChild = true;

public String GetString()
{
xw.WriteEndDocument();
xw.Flush();

ms.Position = 0;

StreamReader sr = new StreamReader(ms);

String XMLString = sr.ReadToEnd();

xw.Close();
ms.Dispose();
sr.Dispose();

return XMLString;
}

public XmlTextWriter GetXmlText()
{
return xw;
}

public XmlDocument GetXmlDoc()
{
xw.WriteEndDocument();
xw.Flush();

ms.Position = 0;

XmlDocument xd = new XmlDocument();
xd.Load(ms);

xw.Close();
ms.Dispose();

return xd;
}

public xOut ParentOf()
{
nextNodeChild = true;

return this;
}

public xOut CloseParent()
{
if (nextNodeChild)
nextNodeChild = false;
else
xw.WriteEndElement();

return this;
}

public xOut XmlDeclaration()
{
ms = new MemoryStream();

xw = new XmlTextWriter(ms, System.Text.Encoding.UTF8);
xw.WriteStartDocument();

return this;
}

public xOut Node(string name)
{
if (nextNodeChild == false)
xw.WriteEndElement();

xw.WriteStartElement(name);

nextNodeChild = false;

return this;
}

public xOut InnerText(string text)
{
return InnerText(text, false);
}

public xOut InnerText(string text, bool useCData)
{
if (useCData)
xw.WriteCData(text);
else
xw.WriteString(text);

return this;
}

public xOut Attribute(string name, string value)
{
xw.WriteAttributeString(name, value);

return this;
}
}

WebDevelopment addons for Firefox

This is my list of required addons for Firefox:
  • Clear Cache Button
  • ColorZilla
  • Firebug
  • MeasureIt
  • Screen grab!
  • View Dependencies
  • Web Developer

Configuring Firefox for Kerberos Authentication

While working at a client's location, I found that they used Kerberos authentication on their LAN. While Firefox can natively use Kerberos authentication, by default it is not turned on. Here is how you can configure Firefox:
  1. Enter about:config in the URL bar
  2. In the filter text box start typing in network.negotiate, to filter the results
  3. Change network.negotiate-auth.delegation-uris to your domain
  4. Change network.negotiate-auth.trusted-uris to your domain
  5. Restart Firefox
That's all there is to it. Now you should be authenticating nicely across the network.

Friday, June 20, 2008

Programming Tools

Expresso - Regex Tool
IE Tester
Ra-Ajax
Dyna-Trace
NUnit
Moq
CodeRush XPress
FxCop
EQATEC Profiler

Thursday, May 15, 2008

Required Add-ons for VS 2005 or IE

Friday, June 30, 2006

Avoiding Duplicate Key Values in Excel

A co-worker was looking to store some records in Excel, but wanted to make sure that there were no duplicate values within the key field. Since this was just a flat record system, converting it to Access of FileMaker Pro seemed like overkill, but we were at a loss as to how to prevent key values in Excel since it is not a database system and does not index. The short answer is, you can't without VBA. However, with a little formula you can help avoid the problem of duplicate entries. This is actually a two step process, though for simplicities sake you only need do the first one.

Conditional Formatting

To avoid duplicates, you can emply conditional formatting. This will enable any duplicate values to show up as a different colour and give you a visual clue that this was a duplicate. The draw back to this step, is it does not prevent duplicates it only warns you that you have made a duplicate but it is still up to you to change the key value so it is no longer a duplicate. Here is how to set up the conditonal formatting:
  1. Go to the cell that will contain the key entry for your first record. (In this example we use row 1 for a header, and column A is where we are typing our key values. So, our first record will go on row 2, since 1 is for headers, making the cell we select A2)
  2. Click on the Format menu, then click on Conditional Formatting... ( a pop-up box will display)
  3. There should be a drop-down box that says "Cell Value Is". Click the down-arrow next to that and change the selection to "Formula Is"
  4. To the right of our drop-down box is where we will type our formula. This is the formula you will want to type out:
    =COUNTIF(A:A,A2)>1
  5. Now that we have told Excel what to look for, we need to tell it how to format the cells. Click on the Format... button in the dialogue box, another pop-up box will display.
  6. Choose your format to anything you want, I chose changing the font to white and then clicked on the Patterns tab and chose a red background, so that the cell and text will really stand out.
  7. Click OK, to close the second dialog box. (You should be able to see a preview of how your new formatting will look, if you don't like it go back and make some changes.) Then, click OK on the first dialog box to return to your spreadsheet.
  8. Without moving the Excel cursor, copy the cell by hitting [CTRL]+[C] on your windows keyboard.
  9. Now, select the entire column by hitting [CTRL]+[SPACEBAR] on your windows keyboard.
  10. We will now paste this formatting to every cell in this column by choosing Edit, from the menu, then clicking on Paste Special...
  11. Select the Format option under the paste header, then click OK
Now, any time you enter a duplicate value the duplicate values will show up with your special formatting.

Data Validation

This technique will prevent anyone from typing in any duplicate values at all. While this may seem the preferable way its drawback is it only works for someone typing in the cell, if they cut & paste the data validation will not work. So, this technique must be used in conjunction with conditional formatting. However, if they do a lot of manual typing to enter the values this is an extra layer of security to insure no human error. Here is the procedure to turn on data validation:
  1. Go to the cell that will contain the key entry for your first record. (In this example we use row 1 for a header, and column A is where we are typing our key values. So, our first record will go on row 2, since 1 is for headers, making the cell we select A2)
  2. Select the Data menu, then Validation...
  3. On the Settings tab, change the Allow drop-down box to Custom.
  4. In the Formula box, enter this formula:
    =(COUNTIF(A:A,A2)>1=FALSE)
  5. Make sure the Ignore Blank check box has a check in it
  6. Click on the Error Alert tab
  7. Our error will be a Stop error, add an appropriate Title (like, Duplicate Data) and explain the error in the Error Message box (I wrote: "You have already used this value, please select another")
  8. Click OK
  9. Without moving the Excel cursor, copy the cell by hitting [CTRL]+[C] on your windows keyboard.
  10. Now, select the entire column by hitting [CTRL]+[SPACEBAR] on your windows keyboard.
  11. We will now paste this formatting to every cell in this column by choosing Edit, from the menu, then clicking on Paste Special...
  12. Select the Validation option under the paste header, then click OK
You have now added a validation option to each cell in your primary field, and any hand entry that results in a duplicate value will not be allowed by Excel. Between both of these options you should be able to succesfully avoid duplicate values in an Excel database without the use of VBA.