Thursday, August 7, 2008

Life: Pollution in China

There are lots of discussions about pollutions in Beijing. Seriously, I think those who haven't been to Beijing had no reason to criticize the situation.
I agree that the air is not as good as Canberra, the sky is not as blue as Australia, but it is no way close to what I've seen in the pictures taken by
Aussie news site such as this post smh.com.au
People have their own judgment and it is not so easy to fool people with such stupid photo, which only shows a cloudy day with fog. If that is what
so called pollution,
  • There are similar pollutions anywhere in the world
  • Could 16 million people possibly live in that dark city without an abnormal death rate?
I spend almost 5 years in Beijing and my lung is not suffering and hey, I am still alive. Oh, I forgot to mention that Beijing Welcome all visitors. Seeing is believing.




using MD5 to compare folder python script

I have been working with linux for almost 1 year now. Still, I like Windows better. Haha

This is a problem i recently trying to solve. When I using cp -r command to copy a folder, or burn a folder onto dvd, i wonder if the copy that i created is 100% correct. To do this, I use md5sum to calculate md5 of the individual files in this folder.

Here is the python script :


#!/usr/bin/python


# perform a shell command and get it's output
from commands import getoutput
# perform a shell command and get it's status (error code)
from commands import getstatusoutput
# get exit, and command line args
from sys import exit,argv

#oldPWD=getoutput('pwd')

p1md5=getoutput('(cd "' + argv[1] + '"; find . ) | sort | md5sum -b')
#print p1md5

p2md5=getoutput('(cd "' + argv[2] + '"; find . ) | sort | md5sum -b')
#print p2md5

#getstatusoutput('cd "' + oldPWD + '"')

if p1md5 == p2md5:
print "Success"
exit (0)
else:
print "Fail"
exit (1)

Wednesday, August 6, 2008

c# .Net XML validate xml file using xsd schema

I talked about using XML file in the last few posts, and especially pointed out the advantage of using xml as interface.
For example: Your mate send you a xml file which contains the information in a format that you defined. You can easily
read and write code without actually getting the xml file because you design the schema.
Now that I successfully create such file, I was wondering the ability to handle errors. I am more aware that what I am gonna
process (xml file) is actually violate the designed format. So I did some research, and did the following things:
  • Generate the schema xsd file for my designed xml file
  • Before processing, using schema xsd file to validate against xml file
  • Report error
Here are some code examples:




if (Validate(strFile, Properties.Settings.Default.schema) != true)
{
MessageBox.Show("XML file doesn't matches the schema! Please verify the input format!");
return;
}

static int numErrors = 0;
static string msgError = "";

private static void ErrorHandler(object sender, ValidationEventArgs args)
{
msgError = msgError + "\r\n" + args.Message;
numErrors++;
}

private static bool Validate(string xmlFilename, string xsdFilename)
{
return Validate(GetFileStream(xmlFilename), GetFileStream(xsdFilename));
}

private static void ClearErrorMessage()
{
msgError = "";
numErrors = 0;
}

// returns a stream of the contents of the given filename
private static Stream GetFileStream(string filename)
{
try
{
return new FileStream(filename, FileMode.Open, FileAccess.Read);
}
catch
{
return null;
}
}

private static bool Validate(Stream xml, Stream xsd)
{
ClearErrorMessage();
try
{
XmlTextReader tr = new XmlTextReader(xsd);
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add(null, tr);

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(schema);
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ErrorHandler);
XmlReader reader = XmlReader.Create(xml, settings);

// Validate XML data
while (reader.Read())
;
reader.Close();

// exception if validation failed
if (numErrors > 0)
throw new Exception(msgError);

tr.Close();
return true;
}
catch
{
msgError = "Validation failed\r\n" + msgError;
return false;
}
}



Tuesday, August 5, 2008

c# .Net XML using XmlDataDocument

Quite often, I need to parse files. I noticed that xml file is really easy to use and simple to define the format.
Last week, I send my mate an xml file scheme and he worked on putting data into xml file. I then read the xml
file and get out the data I need.
Here is some example code:




XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(strFile);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
nsMgr.AddNamespace("def", "http://www.w3.org/2001/XMLSchema");
XmlNodeList nodes = xmlDoc.SelectNodes("/def:xml_DataSet/def:something", nsMgr);
foreach (XmlNode n in nodes)
{
foreach (XmlAttribute a in n.Attributes)
{
switch (a.Name)
{
}
}

foreach (XmlNode m in n.ChildNodes)
{
switch (m.Name)
{
}
}
}



Monday, August 4, 2008

XML Tools Visual XPath

Visual XPath is a graphical way of generating XPath query results. It is a pretty cool that saves you time wondering how can I select a node.
You can download it from http://weblogs.asp.net/nleghari/articles/visualxpath.aspx