SQL: Getting XML Schema from MS SQL Database


It is possible to generate a XML Schema from a Database?

It sure is, XMLSpy can generate XML Schema from a database.

There’s another way, though I’ve never tested it:

create table Person 
Age int not NULL check( Age > 0) , 
Height numeric(10,2) not NULL check( Height > 5), 
Gender varchar(5) not null check( Gender in ('M', 'F', 'O')), 
BirthDate datetime null, 
DECLARE@schema xml 

one more example

DECLARE @schema xml

SET @schema = (SELECT * FROM Company FOR XML AUTO, ELEMENTS, XMLSCHEMA(‘CompanySchema’))

select @schema


C#: Predicate Delegates in C#


Predicate Delegates in C#

Can you explain me;

  • What is Predicate Delegate ?
  • Where should we use predicates ?
  • Any best practices about predicates ?

Descriptive source code will be appreciated,


A predicate is a function that returns true or false. A predicate delegate is a reference to a predicate.

So basically a predicate delegate is a reference to a function that returns true or false. Predicates are very useful for filtering a list of values – here is an example.

using System; using System.Collections.Generic; class Program { static void Main() { List<int> list = new List<int> { 1, 2, 3 }; Predicate<int> predicate = new Predicate<int>(greaterThanTwo); List<int> newList = list.FindAll(predicate); } static bool greaterThanTwo(int arg) { return arg > 2; } } 

Now if you are using C# 3 you can use a lambda to represent the predicate in a cleaner fashion:

using System; using System.Collections.Generic; class Program { static void Main() { List<int> list = new List<int> { 1, 2, 3 }; List<int> newList = list.FindAll(i => i > 2); } } 

Stock Shastra #3: You just need to look at 5 Financial Parameters to shortlist a wonderful company!



So, what comes to your mind when we talk about company financials?

A big, fat, 100-page Annual Report with reams of data that leave most of us confused!  It seems too difficult, too complex, needing too much time – which you don’t have! However, truth is always simple. Let us now see a simple and powerful lens through which we can identify companies with an Excellent Financial Track record. The answer is Stock Shastra #3:  You just need to look at 5 Financial Parameters to shortlist a wonderful company!

From all the 100+ parameters, all you need are just 5 of them! Earnings per share (EPS); Net Sales; Book Value per share (BVPS); Return on Invested Capital (ROIC); and, Debt-to-Net Profit ratio. Seen together and over 10 years these 5 reveal the truth. Let’s see how!


What is the first thing that you will look for in a company before investing? You will check whether it is making Profits, consistently! Since we will be shareholders, we need to look at the profit it earns per share. Hence the first parameter to look for is EPS – Earnings per Share.

How can a company continue to earn profits year after year? By selling more and more every year. Hence, the second parameter to look for is Net Sales.

To increase its sales in the long run, a company will need to expand its capacity. Book Value per Share, BVPS tells us how much a company is investing in expanding its capacity. That’s the third parameter.

Companies in the most basic sense are money-using and money-making machines.  How do we rate a machine? Simple, we look at what it produces in relation to what it uses i.e. efficiency. Companies produce profits using the capital invested (both equity and debt). Hence to know the efficiency with which a company uses its capital, we need to look at Return on Invested Capital (ROIC). That’s the fourth parameter

Finally, if a company borrows money, it should be able to repay it without serious difficulty over a reasonable period of time. Debt-to-Net Profit ratio tells us the number of years in which the company will be able to repay the debt. Hence the fifth parameter to look for is its Debt-to-Net Profit Ratio.


We have checked over 1500 companies and arrived at a gold standard that only the best meet: A company that has been growing its EPS, Net Sales and BVPS by 12%+ year-on-year; has a ROIC of over 12% every year; and can pay off its debt in less than 3 years i.e. a Debt-to-Net Profit ratio of 3 or less – has a great Financial Track Record. Companies meeting this gold standard are wonderful companies worth short listing.
Such companies are quite likely to have a moat – a sustainable competitive advantage, which has allowed it to post great numbers. So, now you know how important these 5 parameters are!

We are sure your next question now is ‘Where do I get these 5 Financial Parameters, without wasting any time?’ It was ours too earlier. So we searched high and low and found that it was not available anywhere. And, we chose the road less travelled – we decided to make it available These crucial 5 parameters, over 10 years are now available as a simple and powerful 10 YEAR X-RAY at MoneyWorks4me.com



Error: C# Collection was modified; enumeration operation may not execute


Simple solution is: When you get this error, don’t you foreach instead use for loop for collections.

Problem: I’m trying to remove a item from a ArrayList and I get this Exception:
Collection was modified; enumeration operation may not execute.
Any ideas? thanks for the help


You are removing the item during a foreach, yes? Simply, you can’t. There are a few common options here:

· use List<T> and RemoveAll with a predicate

· iterate backwards by index, removing matching items

· for(int i = list.Count – 1; i >= 0; i–) {

· if({some test}) list.RemoveAt(i);

· }

· use foreach, and put matching items into a second list; now enumerate the second list and remove those items from the first (if you see what I mean)

For the record, unless you are using .NET 1.0 or 1.1, you should probably be using List<T> instead of ArrayList. – Marc Gravell♦ Jan 7 ’10 at 22:37

C#: FileWatcher: Filtering multiple files using FileSystemWatcher’s filters


Example 1:

Filtering multiple files using FileSystemWatcher’s filters

· using System;

· using System.IO;

· using System.Text.RegularExpressions;


· class MyWatcher

· {


· static void Main(string[] args)

· {

· // if there are no arguments exit

· if (args.Length == 0) return;


· FileSystemWatcher objWatcher = new FileSystemWatcher();

· objWatcher.Path = args[0];

· objWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

· objWatcher.Filter = "*.*";


· // add event handlers

· objWatcher.Changed += new FileSystemEventHandler(OnChanged);

· objWatcher.Created += new FileSystemEventHandler(OnChanged);

· objWatcher.Deleted += new FileSystemEventHandler(OnChanged);

· objWatcher.Renamed += new RenamedEventHandler(OnChanged);



· Console.Write("Watching: ");

· Console.WriteLine(args[0]);


· Console.WriteLine("Press \’q\’ to quit.");

· Console.WriteLine();

· while (Console.Read() != ‘q’);

· }


· private static string getFileExt(string filePath)

· {

· if (filePath == null) return "";

· if (filePath.Length == 0) return "";

· if (filePath.LastIndexOf(".") == -1) return "";

· return filePath.Substring(filePath.LastIndexOf("."));

· }


· private static void OnChanged(object source, FileSystemEventArgs e)

· {

· // get the file’s extension

· string strFileExt = getFileExt(e.FullPath);


· // filter file types

· if (Regex.IsMatch(strFileExt, @"\.txt)|\.jpg", RegexOptions.IgnoreCase))

· {

· Console.WriteLine("A change has been made to a watched file type.");

· }

· }



Example 2: