Friday, 12 October 2007

Build custon XML from database using XmlTextWriter

Simple way to out put as xml is using
dim ds as dataset
ds.WriteXml("C:\test\HCSReports\createdXML\employee.xml")

if you need custom xml output then use the below function



Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl

Public Function CreateCustomXML(ByVal objDataSet As DataSet) As String

Dim intI As Integer
Dim strPath As String

'create an instance of the XmlTextWriter object
Dim objWriter As XmlTextWriter = Nothing

Try

' location to the XML file to write
strPath = strFilepath & Now.Second & ".xml"

objWriter = New XmlTextWriter(strPath, System.Text.Encoding.Default)

' start writing the XML document
objWriter.WriteStartDocument()

' write a comment in our XML file
objWriter.WriteComment("PanelManagerMonthlyReport")

' starting with the root element i.e. "movies"
objWriter.WriteStartElement("PMMonthlyReport")

'Set data to XML tags
If objDataSet.Tables(0).Rows.Count > 0 Then
For intI = 0 To objDataSet.Tables(0).Rows.Count - 1
objWriter.WriteStartElement("Period") ' output the "Employee" element
objWriter.WriteElementString("Name", objDataSet.Tables(0).Rows(intI).Item("Name").ToString)
objWriter.WriteElementString("Prospects", objDataSet.Tables(0).Rows(intI).Item("Prospects").ToString)
objWriter.WriteElementString("Instructions", objDataSet.Tables(0).Rows(intI).Item("Instructions"))
objWriter.WriteElementString("Cancellations", objDataSet.Tables(0).Rows(intI).Item("Cancellations"))
objWriter.WriteElementString("Completions", objDataSet.Tables(0).Rows(intI).Item("Completions"))
objWriter.WriteElementString("Quotes", objDataSet.Tables(0).Rows(intI).Item("Quotes"))
objWriter.WriteElementString("IntroducerUserRegistrations", objDataSet.Tables(0).Rows(intI).Item("IntroducerUserRegistrations"))
objWriter.WriteElementString("IntroducerUserTrained", objDataSet.Tables(0).Rows(intI).Item("IntroducerUserTrained"))
objWriter.WriteElementString("SolicitorAppsIssued", objDataSet.Tables(0).Rows(intI).Item("SolicitorAppsIssued"))
objWriter.WriteElementString("SolicitorContractsReturned", objDataSet.Tables(0).Rows(intI).Item("SolicitorContractsReturned"))
objWriter.WriteElementString("Index", objDataSet.Tables(0).Rows(intI).Item("Index"))
objWriter.WriteEndElement() ' close "Employee" element
Next
End If

' end the "Menu" element
objWriter.WriteEndElement()

' flush and write XML data to the file
objWriter.Flush()

Return strPath
Catch ex As Exception
Return False
Finally
' clear up memory
objWriter.Close()
objWriter = Nothing
objDataSet = Nothing
End Try
End Function

Thursday, 11 October 2007

Design Patterns

http://www.dofactory.com/Patterns/Patterns.aspx#list
scroll down...










































































































  Creational Patterns
  Abstract Factory   Creates an instance of several families of classes
  Builder   Separates object construction from its representation
  Factory Method   Creates an instance of several derived classes
  Prototype   A fully initialized instance to be copied or cloned
  Singleton   A class of which only a single instance can exist
  Structural Patterns
  Adapter   Match interfaces of different classes
  Bridge   Separates an object’s interface from its implementation
  Composite   A tree structure of simple and composite objects
  Decorator   Add responsibilities to objects dynamically
  Facade   A single class that represents an entire subsystem
  Flyweight   A fine-grained instance used for efficient sharing
  Proxy   An object representing another object
  Behavioral Patterns
  Chain of Resp.   A way of passing a request between a chain of objects
  Command   Encapsulate a command request as an object
  Interpreter   A way to include language elements in a program
  Iterator   Sequentially access the elements of a collection
  Mediator   Defines simplified communication between classes
  Memento   Capture and restore an object's internal state
  Observer   A way of notifying change to a number of classes
  State   Alter an object's behavior when its state changes
  Strategy   Encapsulates an algorithm inside a class
  Template Method   Defer the exact steps of an algorithm to a subclass
  Visitor   Defines a new operation to a class without change
'if the enum is :
Public Enum aslamEnum
Created = 0
AwaitingPayment
Active
InProduction
Complete
Cancelled
End Enum


'The function will return description if exist else the name:

Private Function GetEnumDescription(ByVal Value As aslamEnum) As String
Dim objFieldInfo As FieldInfo
Dim sRetVal As String
Dim objDescriptionAttributes() As DescriptionAttribute
Dim tEnum As Type = GetType(aslamEnum)

objFieldInfo = tEnum.GetField([Enum].GetName(tEnum, Value))
objDescriptionAttributes = objFieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False)
With objDescriptionAttributes
If .Length > 0 Then
sRetVal = objDescriptionAttributes(0).Description
Else
sRetVal = [Enum].GetName(tEnum, Value)
End If
End With

Return Trim(sRetVal)
End Function


'To get the value back either in Name or Description use
value=GetEnumDescription(varmyEnum)