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


Sunday, August 3, 2008

Ipod touch 1.1.5 jailbreak using Ziphone

I recently got an ipod touch with firmware 1.1.5.
I tried jailbreak using Ziphone and it works. Basically, nothing specially, all you do is download Ziphone from
http://download.ziphone.org/ and install that.
After you successfully install the software, click jailbreak and that is it.
Good luck.

You might need to fake your firmware version to 1.1.4 using FWChange. (You can find it in Installer)

c# web service authorization method

I'm not an expert on this issue, but what I used in my previous project was the token method.
A token which encrypted by the client needs to be included in every message, and gets decrypted
at the server side. The server will use the decrypted message to verify user. Session is also used to
stop people from keep trying tokens.
Another Important thing is secure transfer. SSL encryption should be used and this can be done in the
IIS server. So, the publish web service should be something start with https://. This way, token won't be
stolen during the transmission.
I'm also thinking to improve the encryption carried out in the client by sending acquiring keys from the server.
The keys will be changed from time to time, and even if Token was leaked, it won't be active for long.