基于 XSD 创建 XML 文件
一篇文章,描述了基于模式创建 XML 文件。
引言
本文将成功地解释如何基于您拥有的模式生成 XML 文件。
背景
我真的花了几天时间在 Google 上搜索并尝试基于我提供的模式生成 XML 文件。我成功地从 SQL 加载了数据集,其中包含生成 XML 文件所需的数据,但我无法将其绑定到我需要的格式。作为一名软件工程师,我想到是否可以硬编码并将其写入文本文件?天哪,我发现了随 Visual Studio SDK 一起提供的 XSD.exe 工具。该工具生成一个类文件,该文件将返回模式中描述的结构的 Dataset。因此,一旦您设置了所有属性,就可以进行操作。
Using the Code
您可以在 Visual Studio SDK 的“bin”文件夹中找到 XSD.exe(如果您没有使用 Visual Studio,我将 EXE 文件附加给您)。您可以使用任何 .NET 支持的语言生成代码(您可以通过在提示符中运行 xsd/?
来找到它们)。由于我使用 VB.NET,我将为您提供 VB 中的示例。我有一个名为“schema.xsd”的模式文件。我将使用以下命令在命令提示符中生成一个类文件“schema.vb”
xsd /d /l:VB C:\schema.xsd /n:XSDSchema.Namespace<!--?xml
namespace="" prefix="O" ?-->
这将生成一个类文件,该文件位于 XSD.exe 存在的位置。复制该文件并将其添加到您使用的类库或项目中,然后您必须将模式中每个元素的列分配给值,从而生成数据集。在查看我附带的示例代码之前,您将无法理解。
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
'adding the header
With _integrationMessage.Header
Dim HeaderRow As IntegrationMessage.HeaderRow
HeaderRow = .NewHeaderRow()
With HeaderRow
.messageType = "xml"
.sendDate = Date.Now
.sender = "Odin"
.Header_Id = 1 'as there will be one header
End With
.Rows.Add(HeaderRow)
End With
'adding the body information
With _integrationMessage.Body
Dim Bodyrow As IntegrationMessage.BodyRow
Bodyrow = .NewBodyRow()
With Bodyrow
.Body_Id = 1 'as there will be one body
End With
.Rows.Add(Bodyrow)
End With
'adding the body contents from the dataset
For Each row As DataRow In ds.Tables(0).Rows
Customer_id = row("Customer_id")
If prevCustomer_id <> Customer_id Then
'the customer has changed therefore
'new customer nodes to be created
'adding the students element
With _integrationMessage.Students
Dim StudentsRow As IntegrationMessage.StudentsRow
StudentsRow = .NewStudentsRow()
With StudentsRow
.Body_Id = 1 'as there will be a single body
.Students_Id = Customer_id
End With
.Rows.Add(StudentsRow)
End With
'adding the student element
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
'adding the student info element
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
'adding the courses which can be multiple for a customer
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
'adding the course entity
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
上面的函数使用从 SQL 加载的 Dataset “ds
” 填充基于模式的 Dataset “IntegrationMessage
”。完成设置所有属性的值后,您可以对 Dataset 可以执行的所有功能进行操作。有趣,不是吗?
注意:具有三层架构工作经验会有所帮助。如果您没有三层架构的经验或了解,那么在开始编码之前,最好先掌握一些相关知识。
关注点
我发现浪费三天时间把最终工具放在我的桌面上是多么糟糕。
历史
请查看我的其他文章,它们可能会对您有所帮助。