Sunday, October 30, 2005

Regular Expression Quick Start

Regular Expression Quick Start
I like it --- way better than MSDN's RegEx "tutorial".

Tuesday, October 18, 2005

Connection string in ASP.NET 2.0 application

It is a good idea to put connection string into connectionStrings section of Web.Config:
<add name="MyConnectionString" connectionString="Integrated Security=SSPI;Initial Catalog=pubs;Data Source=(local);"/>

Then you can easily read it from your application:
string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

If you want to encrypt your connectionStrings section:
1) Copy Web.Config file into C:\Inetpub\wwwroot\MyWebSite folder.
2) Execute the following command:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pe "connectionStrings" -app "/MyWebSite" -prov "DataProtectionConfigurationProvider"

If command from #2 was successfully executed, your ConnectionStrings section in C:\Inetpub\wwwroot\MyWebSite\Web.Config file is encrypted.
You still can use the same command in order to access your connection string:

VS.NET 2005, C#, ASP.NET 2.0

Tuesday, October 11, 2005

How to ignore DTD validation


When I'm trying to load XML into XmlDocument:
or deserialize XML:
XmlSerializer reader = new XmlSerializer(responseType);
object response = reader.Deserialize(xmlReader);

I'm getting an error:
Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\MySchema.dtd'

The problem is the "MySchema.dtd" is referred in XML string/stream which I’m trying to load:
-------- XML string ------

I have MySchema.dtd, but I don’t want to deploy it into .NET folder “'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\”

How could I specify folder where Xml parser should look for DTD schemas?

Or at least how to tell XML parsers to ignore DTD validation comletely?


You may try couple of solutions:
1) Turn off DTD validation completely.
That’s simple: just set XmlResolver property to null:
xmlDocument.XmlResolver = null;
xmlReader.XmlResolver = null;
2) Specify your folder where you store your DTD schemas.
In order to implement it you have to create custom XmlResolver class, that is derive this custom class from XmlResolver, or even better from XmlUrlResolver.
Then override ResolveUri and/or GetEntity methods.
Overridden ResolveUri has to return absolute path to your DTD storage folder.
Last step would be instantiate your custom class and set XmlResolver property:
xmlDocument.XmlResolver = new MyCustomXmlUriResolver();

Example/Examples/Sample for Visual Studio .NET 2005, C# 2.0, ASP.NET 2.0
System.Xml namespace


About Me

My photo
Email me: