CSharp - XML Validation with XML Schema

Posted on January 30th, 2006 by gernot.
Categories: CSharp.
Tags: , , , , ,

This is a short tutorial how one can validate a XML Document against a XML schema by using C# and the .NET framework.

C# Class

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

namespace XMLValidation
{
class XMLValidatorTest
{

private bool XMLisValid = true;

public void ReadValidateXMLFile()
{
   XmlTextReader reader = new XmlTextReader(“C:\\test.xml”);
   XmlValidatingReader validator = new XmlValidatingReader(reader);
   validator.ValidationType = ValidationType.Schema;
   validator.ValidationEventHandler +=
      new ValidationEventHandler(this.XMLValidationEventHandler);

   while (validator.Read())
   {
      //Add code here to process XML content
   }
   validator.Close();

   //Check if XML document is valid or not
   if (this.XMLisValid)
      Console.WriteLine(“XML Document is valid”);
   else
      Console.WriteLine(“XML Document is invalid”);
   }
}

public static void XMLValidationEventHandler(
  object sender,ValidationEventArgs args)
{
   this.XMLisValid = false;
   Console.WriteLine(“XML Validation event” + args.Message);
}

}
}

XML Root Node - XSD Reference

In the XML File:

‹?xml version=”1.0″ encoding=”utf-8″ ?›
‹testNS:testRoot
  xmlns:testNS=”http://www.gego.info/testxmlns”
  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  xsi:schemaLocation=”http://www.gego.info/testxmlns test.xsd”

In the XSD File:

‹?xml version=”1.0″ encoding=”utf-8″ ?›
‹xs:schema
  id=”test”
  targetNamespace=”http://www.gego.info/testxmlns”
  elementFormDefault=”qualified”
  attributeFormDefault=”unqualified”
  xmlns=”http://www.gego.info/testxmlns”
  xmlns:xs=”http://www.w3.org/2001/XMLSchema”

0 comments.

CSharp - Create and Use User Controls

Posted on January 25th, 2006 by gernot.
Categories: CSharp.
Tags: , , ,

This is a short Tutorial how to create and use User Controls in the .NET Framework (C#):

Create and Use User Control in Visual Studio

  1. Create a new Solution: MyMainApplication.sln & MyMainApplication.csproj
    1. Create a new Windows Form: Form1.cs
  2. In Solution:
    1. Add >>
    2. New Project >>
    3. Windows Control Library: MyUserControlLibrary.csproj
  3. In MyMainApplication.csproj:
    1. Right Click References >>
    2. Add Reference >>
    3. Tab: Projects >>
    4. Mark MyUserControlLibrary.csproj >>
    5. OK
  4. In MyUserControlLibrary.csproj:
    1. Right Click on Project >>
    2. Add >>
    3. User Control >>
    4. MyUserControl1.cs
  5. Drag&Drop Controls to MyUserControl1 in Visual Studio Designer (or add manually if you like)
  6. In Visual Studio Menu:
    1. Build >>
    2. Build Solution
  7. In Visual Studio Designer for Form1.cs you can find the MyUserControl1 Control in the toolbox and drag&drop them to your Form
  8. Note: If you make changes to the UserControl (MyUserControl1) you have to rebuild the solution everytime to see the changes in your Main Form (Form1)
csharp-user-control example
Example of User Controls, shown in ToolBox

Event Firing from User Control to Application

Here I want to describe how one can fire an event (e.g.: from a button) in the UserControl and notify the Main Application, which uses the User Control:

In MyUserControl1.cs:

//Just a TestString
public string TestString = “Test123″;
// Declare delegate Button1 clicked.
public delegate void Button1_ClickHandler();
// Declare the event, which is associated with our delegate
[Category("Action")] [Description("Fires when the Button1 is clicked.")]
public event Button1_ClickHandler Button1_Clicked;
// Handler for Button1
// NOTE: Don’t forget to add this Handler to the Button itself

private void Button1_Click(object sender, System.EventArgs e)
{
    if (Button1_Clicked != null)
      // Notify Subscribers
      Button1_Clicked();
    else
      Console.WriteLine(“No Subscribers for Button1″);
}

In Form1.cs:

// NOTE: Don’t forget to add this Handler to the UserControl itself
private void MyUserControl11_Button1_Clicked()
{
    Console.WriteLine(“Button1 was clicked in MyUserControl11, TestString:”
    + MyUserControl11.TestString);
}

0 comments.

lab2004 - Vortex

Posted on February 16th, 2005 by gernot.
Categories: Studies, work.
Tags: ,

The lab2004 - project is held by the TU Vienna and Microsoft Austria.
My part was leading the development team, which job it was to develop a new kind of course management tool for students and teachers.

Vortex

  • The Student Portal for students
  • developed with the newest technology

Racoon

  • INFLAB (Client Deployment)
  • runs on the newest technology alpha, beta builds)

Build a software that …

  • is expandable and customizable.

Build a student portal that …

  • you want to have.
  • streamlines workflows for every user group.
  • has quite a lot state-of-the-art features ( mySite, portability…).

Vortex - Team:

  • 3 Managers. My postiton: Development Manager
  • 20 Team Members
  • 15 Developers
  • 5 Managment Support

Involving 2 fields of study

  • Computer science
  • Computer science in economics

Vortex Outcome:

NET Web - Application

  • Tool for teachers and students

State of the Art Features

  • Web Standards (XHTML, CSS) lead to
    • simple adaptation of design
    • portability (Handhelds,…)
  • Including AD leads to extensibility
  • Internationalization (english, german,…)

0 comments.