Misadventures with xsd.exe


I am writing a reporting tool and I got the required schema as a set of xsd files. Open the Visual Studio command line and run xsd.exe on the outer schema specifying /c to build classes. I get this nice 12k line code file. Ok so this is going to be a breeze right?

Ok so I pop into a C# project place my generated source file and start populating objects as a test. I serialize to a xml file and it looks pretty good. Then I notice the BeginDate field is missing. Lets take a look at the generated code. 
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime BeginDate{
get { return this.BeginDateField;}
set { this.BeginDateField = value;}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool BeginDateSpecified{
get { return this.BeginDateFieldSpecified;}
set { this.BeginDateFieldSpecified = value;}
}
 That's weird look at that BeginDateSpecified boolean property.. Hmmm. That's a funny way of doing things. Lets take a look at the xsd for this element.
<xsd:element name="BeginDate" type="DateType" minOccurs="0"/>
No mention of "BeginDateSpecified" in the xsd file. Ok I will play along.

NameSpecified = true;
I run the XmlSerializer again and now the field is there.

Ok so for every element that has the attribute minOccurs="0" in the xsd I am going to have to tell the serializer whether or not it has a value? That won't do.

Let's see what we can do.

[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime? BeginDate{
get { return this.BeginDateField;}
set { this.BeginDateField = value;}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool BeginDateSpecified{
get { return this.BeginDate != null;}
}

Ok so if the property is persisted as an element we can change it to a nullable type and then in the BeginDateSpecified property can just check for null.

We can use this regex to search
(?^[^\S\r\n]*public[^\S\r\n])(?(System\.)*\w*)[^\S\r\n](?\w*)(?\r?\n[^\S\r\n]*{\r?\n[^\S\r\n]*get\r?\n[^\S\r\n]*{\r?\n[^\S\r\n]*return[^\S\r\n]this.\w*;\r?\n[^\S\r\n]*}\r?\n[^\S\r\n]*set\r?\n[^\S\r\n]*{\r?\n[^\S\r\n]*this.\w*[^\S\r\n]=[^\S\r\n]value;\r?\n[^\S\r\n]*}\r?\n[^\S\r\n]*}\r?\n\r?\n[^\S\r\n]*///[^\S\r\n]\\r?\n[^\S\r\n]*\[System\.Xml\.Serialization\.XmlIgnoreAttribute\(\)\]\r?\n[^\S\r\n]*public[^\S\r\n]bool[^\S\r\n]\w*Specified\r?\n[^\S\r\n]*{\r?\n[^\S\r\n]*get\r\n[^\S\r\n]*{\r?\n[^\S\r\n]*return[^\S\r\n]this.)\w*(?;\r?\n[^\S\r\n]*}\r?\n)[^\S\r\n]*set\r?\n[^\S\r\n]*{\r?\n[^\S\r\n]*this.\w*[^\S\r\n]=[^\S\r\n]value;\r?\n[^\S\r\n]*}\r?\n
(if you copy this be aware there should be no spaces in the regex search code) and replace it with this.
${start}${type}? ${name}${body}${name} != null${ending}
You will have to manually change the backing field to nullable. I couldn't come up with an easy way to change that through find replace.

If any of the properties are serialized as attributes this won't work because you aren't allowed to use nullable types there. Luckly I only had 3 of those cases and they were all enums.
[System.Xml.Serialization.XmlAttributeAttribute()]
public GenderType gender {
get { return this.genderField;}
set { this.genderField = value;
this.genderFieldSpecified = (value != 0);
}
}

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool genderSpecified {
get { return this.genderFieldSpecified;}
}
I then modified the enum to give the first enum a value of 1
public enum GenderType
{
Male = 1,
Female,
}

Posted in Software Development | Tagged , , | Leave a comment

Give your body what it needs


I am a professional software developer. This means I sit at a desk most of the day. I am around my normal weight for my height and build. I don’t workout or go to a gym.

A few years ago I noticed especially in the summer and fall months I was feeling very tired most of the day. I did a little research and read if you don’t consume enough water you will feel tired and lethargic. I added water to my diet and it helped but I still didn’t have tons of energy. I would notice this more in the afternoon after lunch.

I have never been one to put much stock in vitamins supplements. I will occasionally take an Aleve for a headache but that was about it. About 16 months ago I started having problems with hemorrhoids and started taking Rutin as a supplement for capillary health. After a few weeks the problems mainly went away. As long as I keep taking the rutin I am happy. This made me change my mind about supplements. I saw there are nutrients that our bodies are deficient in because of our diet and lifestyle.

Over the next year when I felt sick I would turn to a herbal remedy. A few months ago a colleague inquired about what I take for energy or focus. I hadn’t even thought about taking a supplement for focus or energy. I started doing some research and came across Now Foods brand “Energy” and “True Focus“. Now Foods is the company that makes the Rutin I had been taking so I trusted them.

I added 2 Energy and 2 true focus to my morning routine. It doesn’t give you the kind of energy a drug would because it isn’t trying to trick your mind and body. I see it as balancing things out. I normally take another 1 Energy and 1 true Focus after lunch. I don’t feel drained now like I did before.

I then got my wife started on the same regimen. After a few weeks I asked her what she thought about it and she stated she didn’t think she could tell much difference. One day she forgot to take them and said she felt like dying. I bet she hasn’t skipped a day since.

Recently I have added 1 “Immune Renew” to my morning regimen to try and ward off any sickness.

Posted in Health | Tagged , , , | 1 Comment

JavaScript Leftovers


The script bundler in MVC 4 can be a good or a bad thing. When you direct it to load a js file with wildcards you could get more than you want. I was working on a project with out of date jQuery files. I let Nuget update them to the latest version of Jquery. Looking at the project script folder everything looked good and I had the latest version of jQuery. Unbeknownst to me the actual disk folder now contained both the old and new version. So now the bundler was loading both versions. If you are using the browser when debugging Visual Studio will show you the loaded scripts in the solution explorer. The lesson learned from this is when updating js scripts make sure the old versions aren’t still hanging around.

Posted in Software Development | Tagged , , | Leave a comment

Implicit Value Assignment Pitfalls in VB.net


Look at the following block of VB.net code.

For i = 1 To 10
    Dim isEven As Boolean
    If i Mod 2 = 0 Then
        isEven = True
    End If
    Console.WriteLine("{0} {1}", i, isEven)
Next

Do you think the value isEven will be be correct?
If you said yes you are incorrect. The following block of code will return the same values as the first block.

Dim isEven As Boolean
For i = 1 To 10
    If i Mod 2 = 0 Then
        isEven = True
    End If
    Console.WriteLine("{0} {1}", i, isEven)
Next

The only change is the scope of isEven.
If you modify the first block to explicity state a value for isEven

For i = 1 To 10
    Dim isEven As Boolean = False
    If i Mod 2 = 0 Then
        isEven = True
    End If
    Console.WriteLine("{0} {1}", i, isEven)
Next

The isEven = False is evaluated on each loop and the program will give you the desired results. Had you used C# this wouldn't be a problem because it doesn't allow implicit value assignment. The first two blocks would not have even compiled had you been using C#. The lesson I get from this is to always explicitly set your variable values. The outcome otherwise may not be what you expect.

Posted in Software Development | Tagged , | Leave a comment

Windows Credential Manager


In Windows 7 Microsoft introduced Credential Manager to keep you network passwords. If you connect to a network device and the OS isn’t remembering your password. Click Start and type Credential Manager and then enter. In the Credential Manager dialog add a Windows credential. The Network address is the name of the device or computer you are connecting to. The username is  deviceName\username. For example if I had a username of DarinClark connecting to a device named CrystalLight the username would be CrystalLight\DarinClark. Then enter your password and click save.

Posted in Microsoft Windows | Leave a comment

Whats up with Airline Ticket Prices?


In the past when I was planning a trip I would try and purchase my airline tickets as early as possible. I was under the assumption the earlier the better.

This spring I started watching ticket prices for a trip we are taking to Arizona in July. I discovered prices are very volitile. I saw time periods when American was over 100 dollars cheaper than the other carriers. I would check Southwest occasionally but they were never the best value.

On May 10 the prices dipped again and Delta was about 310 for a 6 to 8 hour duration with 1 stop. Before I committed I checked SouthWest once again and they had dropped to 288 for a nonstop 3 1/2 hour flight, so I went with them.

Over the course of the weeks that followed I continued to check prices. The next day SouthWest was up to 382. My thinking at the time was I had dodged a bullet. By early the next week they dropped to 288 again.  After that they continued to climb but with daily fluctuation.  We are now 2 weeks out and Delta is 444 for a 8 to 9 hour duration and SouthWest is over 700 dollars.

I think you should start your search around 2 1/2 months out to establish a baseline.  Check every day for 2 weeks and somewhere in there you should find the best price.

Posted in travel | Leave a comment

Forgotten Excel worksheet password


Sub PasswordBreaker()
‘Breaks worksheet password protection.
Dim i As Integer, j As Integer, k As Integer
Dim l As Integer, m As Integer, n As Integer
Dim i1 As Integer, i2 As Integer, i3 As Integer
Dim i4 As Integer, i5 As Integer, i6 As Integer
On Error Resume Next
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
ActiveSheet.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If ActiveSheet.ProtectContents = False Then
MsgBox “One usable password is ” & Chr(i) & Chr(j) & _
Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Exit Sub
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
End Sub

Posted in Microsoft Office | Leave a comment