Introduction
This article will be successful in explaining how to generate an XML file based on the schema you have.
Background
I really had a couple of bad days Googling and trying to generate an XML file based on the schema I am given. I successfully loaded the dataset from SQL which contains the data necessary for generating the XML file but I was unable to bind it to the format I needed. Being a software engineer, I thought if it was possible to hard-code it and write it as a text file? Good heavens, I came across the XSD.exe tool which comes along with the SDK of Visual Studio. The tool generates a class file which will return a dataset of the structure described in the schema. So once you have set all the properties, you can play around.
Using the Code
You can find XSD.exe in the "bin" folder of the SDK of Visual Studio (if you are not using Visual Studio, I am attaching the EXE file for you). You can generate the code in any .NET supported language (you can find them by running xsd/?
in the prompt). As I work with VB.NET, I will provide you the sample in VB. I have a schema file named "schema.xsd". I will generate a class file "schema.vb" using the following command in command prompt:
xsd /d /l:VB C:\schema.xsd /n:XSDSchema.Namespace<!--?xml
namespace="" prefix="O" ?-->
This will generate a class file in the location where the XSD.exe exists. Copy the file and add it to the class library or the project you use and then you have to assign the columns in each elements of the schema with the values and thus the dataset gets generated. You will not have any idea until you look into the sample code I have attached.
Private Function FillDataset(ByVal ds As DataSet) As String
Dim _integrationMessage As New IntegrationMessage()
Dim prevCustomer_id As Integer = -1
Dim Customer_id As Integer
With _integrationMessage.Header
Dim HeaderRow As IntegrationMessage.HeaderRow
HeaderRow = .NewHeaderRow()
With HeaderRow
.messageType = "xml"
.sendDate = Date.Now
.sender = "Odin"
.Header_Id = 1
End With
.Rows.Add(HeaderRow)
End With
With _integrationMessage.Body
Dim Bodyrow As IntegrationMessage.BodyRow
Bodyrow = .NewBodyRow()
With Bodyrow
.Body_Id = 1
End With
.Rows.Add(Bodyrow)
End With
For Each row As DataRow In ds.Tables(0).Rows
Customer_id = row("Customer_id")
If prevCustomer_id <> Customer_id Then
With _integrationMessage.Students
Dim StudentsRow As IntegrationMessage.StudentsRow
StudentsRow = .NewStudentsRow()
With StudentsRow
.Body_Id = 1
.Students_Id = Customer_id
End With
.Rows.Add(StudentsRow)
End With
With _integrationMessage.Student
Dim StudentRow As IntegrationMessage.StudentRow
StudentRow = .NewStudentRow()
With StudentRow
.Students_Id = Customer_id
.studentId = Customer_id
.errorMessage = ""
.errorNumber = -1
End With
.Rows.Add(StudentRow)
End With
With _integrationMessage.StudentInfo
Dim StudentInfoRow As IntegrationMessage.StudentInfoRow
StudentInfoRow = .NewStudentInfoRow()
With StudentInfoRow
.studentId = Customer_id
.isActive = IIf(row("Customer_isActive") = 1, True, False)
.firstName = row("FirstName")
.lastName = row("LastName")
.homeCountry = row("HomeCountry")
.email = row("Email")
.startDate = row("StartDate")
.endDate = row("EndDate")
.currentLevel = row("CurrentLevel")
.bookedDate = row("BookedDate").bookedDate = row("BookedDate")
.cancelledDate = row("CancelledDate")
.SchoolCode = row("SchoolCode")
.programCode = row("ProgramCode")
.courseTypeCode = row("CourseTypeCode")
.courseIntensityCode = row("CourseIntensityCode")
.languageCode = row("LanguageCode")
.updateDate = row("CustomerUpdateDate")
.RedemptionCode = row("RedemptionCode")
End With
.Rows.Add(StudentInfoRow)
End With
End If
With _integrationMessage.Courses
Dim CoursesRow As IntegrationMessage.CoursesRow
CoursesRow = .NewCoursesRow()
With CoursesRow
.Courses_Id = row("Course_id")
.studentId = Customer_id
End With
.Rows.Add(CoursesRow)
End With
With _integrationMessage.Course
Dim CourseRow As IntegrationMessage.CourseRow
CourseRow = .NewCourseRow()
With CourseRow
.Courses_Id = row("Course_id")
.courseId = row("Customer_id")
.isActive = IIf(row("Course_isActive") = 1, True, False)
.language = row("LanguageCode")
.SchoolCode = row("SchoolCode")
.startDate = row("StartDate")
.updateDate = row("CourseUpdateDate")
End With
.Rows.Add(CourseRow)
End With
prevCustomer_id = Customer_id
Next
Return _integrationMessage.GetXml()
End Function
The above function fills the schema based dataset "IntegrationMessage
" with the dataset "ds
" which I loaded from SQL. Once you are done with setting all the attributes with the values, you can play around with all the functionalities you can do with the dataset. Interesting! Isn't it?
Note: Having an experience in working with three tier architecture will help you. If you don’t have an experience or an idea of what three tier architecture is, then it will be better that you grasp some knowledge on it before you get into coding.
Points of Interest
I found out how bad it is to waste three days keeping the ultimate tool in my desktop.
History
Please do check out my other articles, which may help you out.