我正在开发一个小型应用程序(在 VB.net 中),用于计算结构的一些机械性能。为此,我需要一个具有一些材料属性的库。 我只是把编程当作一种爱好,所以我对这门语言的所有可能性都不太熟悉。
我有一些想法: 创建一个数组,例如:
Public Shared arrMaterials(,) As String =
New String(,) {{"S235JR", "210000", "235", "360"},
{"S355JR", "210000", "355", "490"},
将我的材料放入 XML 中并读取所需的属性。
但是,我想知道是否(以及如何)可以使用具有属性的自定义对象创建库。
是不是就这么简单:
Dim Material0 As New Material
With Material0
.MaterialName = "S235JR"
.Young-modulus = "210000"
.YieldStrength = "235"
.TensileStrength = "360"
End With
或者我错过了什么?有人可以将我推向正确的方向吗?
答案 0 :(得分:1)
下面的代码演示了如何使用类来获取正确的 XML 结构。它还展示了如何序列化数据(到 XML 文件)以及如何反序列化 XML(从 XML 文件加载数据)。
创建新的 Windows 窗体项目
添加参考:System.Runtime.Serialization
添加引用:System.Xml
创建两个类来保存数据。添加构造函数,方便添加数据。
添加类:XmlMaterial
XmlMaterial.vb
Imports System.Runtime.Serialization
<DataContract(Name:="Material")>
Public Class XmlMaterial
<DataMember(Name:="MaterialName")>
Public Property MaterialName As String
<DataMember(Name:="TensileStrength")>
Public Property TensileStrength As Integer
<DataMember(Name:="YoungModulus")>
Public Property YoungModulus As Integer
<DataMember(Name:="YieldStrength")>
Public Property YieldStrength As Integer
Public Sub New()
End Sub
Public Sub New(ByVal materialName As String, ByVal youngModulus As Integer, ByVal YieldStrength As Integer, ByVal tensilStrength As Integer)
Me.MaterialName = materialName
Me.YoungModulus = youngModulus
Me.YieldStrength = YieldStrength
Me.TensileStrength = tensilStrength
End Sub
End Class
为了使 XML 文件具有正确的结构,创建另一个具有 XmlMaterial 列表的类。添加方法 (AddMaterial) 以便更轻松地添加数据。
添加类:XmlRoot
XmlRoot.vb
Imports System.Runtime.Serialization
<DataContract(Name:="Root")>
Public Class XmlRoot
<DataMember(Name:="Materials")>
Public Property Materials As New List(Of XmlMaterial)
Public Sub AddMaterial(ByVal materialName As String, ByVal youngModulus As Integer, ByVal yieldStrength As Integer, ByVal tensilStrength As Integer)
Materials.Add(New XmlMaterial(materialName, youngModulus, yieldStrength, tensilStrength))
End Sub
End Class
添加模块:HelperSerialization
“HelperSerialization.vb”中的代码用于写入XML文件(序列化)和从XML文件读取数据(反序列化)。
HelperSerialization.vb
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml
Module HelperSerialization
Public Function DeserializeXml(Of T)(ByVal filename As String) As T
Dim result As T = CType(Nothing, T)
If String.IsNullOrEmpty(filename) Then
Debug.WriteLine("Error: Filename is null or empty")
Return result
End If
Try
'create new instance
Dim mySerializer As New DataContractSerializer(GetType(T))
'read XML file
Using fs As FileStream = New FileStream(filename, FileMode.Open)
Using reader As XmlReader = XmlReader.Create(fs)
Return mySerializer.ReadObject(reader)
End Using
End Using
Catch ex As System.Runtime.Serialization.SerializationException
Debug.WriteLine("Error (DeserializeXml - SerializationException) - " & ex.Message)
Catch ex As Exception
Debug.WriteLine("Error (DeserializeXml) - " & ex.Message)
End Try
End Function
Public Sub SerializeToXmlFile(ByVal filename As String, ByVal xmlObject As Object)
If String.IsNullOrEmpty(filename) Then
Debug.WriteLine("Error: Filename is null or empty")
Return
End If
Try
'create new instance
Dim mySerializer As New DataContractSerializer(xmlObject.GetType())
Dim myXmlWriterSettings As New XmlWriterSettings
With myXmlWriterSettings
.OmitXmlDeclaration = False
.Encoding = System.Text.Encoding.UTF8
.Indent = True
End With
'write to XML file
Using myXmlWriter As XmlWriter = XmlWriter.Create(filename, myXmlWriterSettings)
'serialize
mySerializer.WriteObject(myXmlWriter, xmlObject)
End Using
Catch ex As System.Runtime.Serialization.SerializationException
Debug.WriteLine("Error (SerializeToXmlFile - SerializationException) - " & ex.Message)
Catch ex As Exception
Debug.WriteLine("Error (SerializeToXmlFile) - " & ex.Message)
End Try
End Sub
End Module
向表单添加按钮(名称:btnSave)
向表单添加按钮(名称:btnLoad)
注意:双击每个按钮创建点击处理程序
Form1.vb
Public Class Form1
Private ConstructionMaterials As New XmlRoot
Private filename As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "materials.xml")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateTestData()
End Sub
Private Sub CreateTestData()
ConstructionMaterials.AddMaterial("S235JR", "210000", "235", "360")
ConstructionMaterials.AddMaterial("S123JR", "200000", "212", "300")
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
'serialize - write to file
SerializeToXmlFile(filename, ConstructionMaterials)
Debug.WriteLine("Data saved to: " & filename)
End Sub
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
'deserialize - read from file
ConstructionMaterials = DeserializeXml(Of XmlRoot)(filename)
If ConstructionMaterials IsNot Nothing AndAlso ConstructionMaterials.Materials.Count > 0 Then
Debug.WriteLine("Data read from: " & filename)
End If
If ConstructionMaterials IsNot Nothing Then
For i As Integer = 0 To ConstructionMaterials.Materials.Count - 1
Debug.WriteLine(ConstructionMaterials.Materials(i).MaterialName & " " & ConstructionMaterials.Materials(i).TensileStrength)
Next
End If
End Sub
End Class
上面的代码将创建以下 XML 文件。
materials.xml
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MaterialProject">
<Materials>
<Material>
<MaterialName>S235JR</MaterialName>
<TensileStrength>360</TensileStrength>
<YieldStrength>235</YieldStrength>
<YoungModulus>210000</YoungModulus>
</Material>
<Material>
<MaterialName>S123JR</MaterialName>
<TensileStrength>300</TensileStrength>
<YieldStrength>212</YieldStrength>
<YoungModulus>200000</YoungModulus>
</Material>
</Materials>
</Root>