Monday, April 30, 2007

MIX 07 conference

Visit MIX 07 conference at - http://visitmix.com
Check out the cool videos, sessions, hot topics and debates posted here.
Cheers - Dipesh

Silverlight and Expression Studio News... 1.0 Beta released

This MIX 07 conference has got something exciting in form of Siverlight.

Silverlight is a cross-browser, cross-platform plug-in for delivering the next generation of media experiences and rich interactive applications (RIAs) for the Web. The Silverlight 1.0 Beta has a go-live license that implies it can be used for commercial purposes.

What has it got for you?
· Cross platform .net framework through Silverlight (full Visual Studio experience)
· Perf when using C# instead of js to create rich internet apps is 300-1000 times faster
· Silverlight Visual Studio add-in enables debugging – even remotely debug an app running on a Mac (Matt saw it live!)
· Multiple languages are supported: JavaScript, Ruby, Python, C#, VB .NET, etc

Check this @ http://www.microsoft.com/silverlight/default01.aspx
Download ... It’s free: http://silverlight.live.com/

Cheers - Dipesh

Thursday, April 26, 2007

Ent Libs - How to use Logging block?

Did you know how easy it was to use Ent Libs. In this post i am showing you how to use Logging block. You need to install Ent Libs 2.0 if you are using .net 2.0. Ent Libs 3.0 is out for .net 3.0. You then open Enterprise library configuration tool to create a new app.config or specify your application config file to be modified. It is here you will specify how you want to log.... File, Event, Database or other different listeners provided. This tool is highly configurable so feel free to explore a little. This post will show you how to log your errors/messages to a text file. Once you are configured through this config file you just have to pretty much write 2 lines of code. Easy..is it? Super Easy! :)


C# Code -
private static FlatFileTraceListener nFile;

private static void WriteLog(string sComments)
{
Console.WriteLine(sComments);
//Logger.Write(sComments);
if (nFile != null)
{
nFile.Write(DateTime.Now.ToString() + ": ");
nFile.WriteLine(sComments);
nFile.Flush();
}
}
Your Config file should be with entries of Ent Libs and trace listeners based on the settings we did above using the Ent Libs GUI tool.
Cheers - Dipesh

Validate XML against your schema XSD

Hi - This post is basically to demenstrate how you can validate your XML input against a specified XML schema. The sample below uses new .net 2.o objects rather than obsolete .net 1.1 objects. I have commented few obsolete objects to highlight the differences.

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

namespace ConsoleApplication3
{
///
/// Summary description for Class1.
///


class Class1
{
///
/// The main entry point for the application.
///


System.Boolean m_success;
[STAThread]
static void Main(string[] args)
{

//XmlValidatingReader reader = null; //obsolete in .net 2
XmlReader reader1 = null;

//XmlSchemaCollection myschema = new XmlSchemaCollection(); //obsolete in .net 2
XmlSchemaSet myschema1 = new XmlSchemaSet();
ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors);

try
{
//Create the XML fragment to be parsed.

// String xmlFrag = "YOUR XML string here" ;

//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

//Implement the reader.
//reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context); //obsolete in .net 2.0
XmlReaderSettings rs = new XmlReaderSettings();

rs.ValidationType = ValidationType.Schema;
myschema1.Add("http://www.newmarketinc.com", Server.Mappath("DirectBook.xsd"));
rs.Schemas.Add(myschema1);
StringReader srXml = new StringReader(xmlFrag);
reader1 = XmlReader.Create(srXml, rs, context);

//Add the schema.
//myschema.Add("http://www.newmarketinc.com", "D:\\Documents and Settings\\Administrator\\Desktop\\MI\\DirectBook.xsd");

//Set the schema type and add the schema to the reader.
//reader.ValidationType = ValidationType.Schema;
//reader.Schemas.Add(myschema);
while (reader1.Read())
{ //empty loop;
}
Console.WriteLine("Completed validating xmlfragment");
}
catch (XmlException XmlExp)
{
Console.WriteLine(XmlExp.Message);
}
catch (XmlSchemaException XmlSchExp)
{
Console.WriteLine(XmlSchExp.Message);
}
catch (Exception GenExp)
{
Console.WriteLine(GenExp.Message);
}
finally
{
Console.Read();
}
}
public static void ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine("Validation Error: {0}", args.Message);
}
}
}

References -
.net 1.1 - http://www.codeproject.com/cs/webservices/XmlSchemaValidator.asp,
http://support.microsoft.com/kb/318504

.net 2.0 - http://aspalliance.com/941

Cheers - Dj

XSD Element

The any element enables us to extend the XML document with elements not specified by the schema!

The any element enables us to extend the XML document with elements not specified by the schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the element we can extend (after lastname) the content of "person" with any element:

[person - last name, firstname, any]
any can contain any additonal information (any other element from other schema as well)
So you can define another child.xsd and add it in your XML
Your XML can contain something like -
[person - lastname, firstname, [children - childname] ]
Its little difficult to format the XML output in this blog so please reference this link - http://www.w3schools.com/schema/schema_complex_any.asp

BTW, w3schools is the best site for all your questions regarding XML schema.
Cheers - Dj

Monday, April 23, 2007

नमस्ते !
This small piece of code is very helpful when you want to do repetative tasks of Fetching an XML and forming a dataset out of it.

public DataSet FetchDataSet(string fetchXml)
{
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
string strResult = service.Fetch(fetchXml);

DataSet ds = new DataSet();
StringReader reader = new StringReader(fetchXml);
ds.ReadXml(reader);
return ds;
}

Cheers - Dipesh

Tuesday, April 10, 2007

Latest Release: Enterprise Library 3.0 – April 2007 (for .NET Framework 2.0 and 3.0)

Here it is...
http://msdn2.microsoft.com/en-us/library/aa480453.aspx

Download Ent Libs 3.0 for .net framework 2.0 and 3.0

Cheers - Dipesh
Long time, tied up with some work... but i am going to keep this rolling.
Let's take a light topic... do you know how to Read and write web App.config files?
Here it is...
Read Config -
static void ReadConfig()
{
foreach(string key in ConfigurationManager.AppSettings)
{ string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
}

Write Config -
static void WriteConfig() {
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Add an Application Setting.
config.AppSettings.Settings.Add("ModifiedDate", DateTime.Now.ToLongTimeString() + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
}}


Output:
Key: Settings1, Value: Value1
Key: Modified Date, Value: 12:00:00


Ok, did you figure that the app.config does not stick values for the keys that you added or for the keys that you modified. Check the app.exe.config in the debug or release version. You should find it stick there. I foudn it little wierd but not sure when in debug mode i could not find app.config modified but had app.exe.config modified. If anyone you have had the same experience and seems that i might have missed something then please let me know.

Well, before i end you have another option as Settings file that can be used to get/set settings at user/application level for your application as well.

Thanks - Dipesh