我想用一个看起来像XML的字符串数据填充一个对象。我已经尝试过使用RegEx并且它有效,但它非常难看。
我的字符串如下所示:
<Item1*>XXXXXX</Item1*>
<Item2*>XXXXXX</Item2*>
<DOC*>
<DOC_ID*>XXXXXX</DOC_ID*>
<DOC_TYPE*>XXXXX</DOC_TYPE*>
</DOC*>
<HU*>
<HU_X1*>XXXXXX</HU_X1*>
<HU_Y1*>XXXXXX</HU_Y1*>
<HU_Z1*>XXXXXX</HU_Z1*>
</HU*>
<HU*>
<HU_X2*>XXXXXX</HU_X2*>
<HU_Y2*>XXXXXX</HU_Y2*>
<HU_Z2*>XXXXXX</HU_Z2*>
</HU*>
<HU*>
<HU_X3*>XXXXXX</HU_X3*>
<HU_Y3*>XXXXXX</HU_Y3*>
<HU_Z3*>XXXXXX</HU_Z3*>
</HU*>
我丑陋功能的一个简短例子:
Public Function toObject(ByVal Str_ShipData As String) As ShipData
Dim shipData As New ShipData
Dim regex As New System.Text.RegularExpressions.Regex("<Item1\*>(.+?)<\/Item1\*>")
Dim match = regex.Match(Str_ShipData)
shipData.SHIPMENT_REF_1 = match.Groups(1).Value()
regex = New System.Text.RegularExpressions.Regex("<Item2\*>(.+?)<\/Item2\*>")
...
Return shipData
End Function
有更好的方法吗?
编辑**
我尝试使用XMLDocument但是我发现了以下错误:
There is an error in XML document (1, 2)
<Shipdata xmlns = ''> was unexpected.
我的新功能:
Public Function XMLtoObject(ByVal Str_ShipData As String) As ShipData
Dim shipData As New ShipData
Str_ShipData = "<Shipdata>" & Str_ShipData.Replace("*", "") & "</Shipdata>"
Dim serializer As XmlSerializer = Nothing
Dim strReader As StringReader = Nothing
Dim xmlReader As XmlTextReader = Nothing
Dim doc As New XmlDocument()
serializer = New XmlSerializer(GetType(ShipData))
doc.LoadXml(Str_ShipData)
strReader = New StringReader(doc.OuterXml)
xmlReader = New XmlTextReader(strReader)
shipData = serializer.Deserialize(xmlReader)
Return shipData
End Function
XML输出:
<?xml version="1.0"?>
<Shipdata>
<Item1*>XXXXXX</Item1*>
<Item2*>XXXXXX</Item2*>
<DOC*>
<DOC_ID*>XXXXXX</DOC_ID*>
<DOC_TYPE*>XXXXX</DOC_TYPE*>
</DOC*>
<HU*>
<HU_X1*>XXXXXX</HU_X1*>
<HU_Y1*>XXXXXX</HU_Y1*>
<HU_Z1*>XXXXXX</HU_Z1*>
</HU*>
<HU*>
<HU_X2*>XXXXXX</HU_X2*>
<HU_Y2*>XXXXXX</HU_Y2*>
<HU_Z2*>XXXXXX</HU_Z2*>
</HU*>
<HU*>
<HU_X3*>XXXXXX</HU_X3*>
<HU_Y3*>XXXXXX</HU_Y3*>
<HU_Z3*>XXXXXX</HU_Z3*>
</HU*>
</Shipdata>
要解决此错误,请使用以下命令:
serializer = New XmlSerializer(GetType(ShipData), New XmlRootAttribute("Shipdata"))
其他问题:序列化程序没有填写我的HU列表
Public Property HU As List(Of HU)
Get
Return oHUS
End Get
Set(ByVal value As List(Of HU))
oHUS = value
End Set
End Property
答案 0 :(得分:2)
如果您有带标记的字符串并且您想要填充对象,请使用XML而不是Regexp。
为此,请点击此链接:https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx
使用字符串和填充对象创建XML文档的通用函数。
Public Function XMLToObject(ByVal Str_Xml As String, ByVal objectType As Type) As [Object]
Dim obj As [Object] = Nothing
Dim serializer As XmlSerializer = Nothing
Dim strReader As StringReader = Nothing
Dim xmlReader As XmlTextReader = Nothing
Dim doc As New XmlDocument()
'serializer = New XmlSerializer(GetType(ShipData), New XmlRootAttribute("YOUROOT")) 'If you have root problem
serializer = New XmlSerializer(GetType(ShipData))
doc.LoadXml(Str_Xml)
strReader = New StringReader(doc.OuterXml)
xmlReader = New XmlTextReader(strReader)
obj = serializer.Deserialize(xmlReader)
Return obj
End Function
对于列表属性,请在您的媒体资源上添加XmlElement。
链接可以帮助您:Creating XML structure using xml attributes
<XmlElement(ElementName:="HU", Type:=GetType(HU))> _
Public Property HU As List(Of HU)
Get
Return oHUS
End Get
Set(ByVal value As List(Of HU))
oHUS = value
End Set
End Property
答案 1 :(得分:1)
使用<XmlElement>
属性来序列化属性。类型是自动推断的;无需指定。如果xml元素名称与属性名称不同,请使用<XmlElement("elementName")>
。
Public Class Shipdata
<XmlElement>
Public Property Item1 As String
<XmlElement>
Public Property Item2 As String
<XmlElement>
Public Property DOC As DOC
<XmlElement("HU")>
Public Property HUs As List(Of HU)
End Class
Public Class DOC
<XmlElement>
Public Property DOC_ID As String
<XmlElement>
Public Property DOC_TYPE As String
End Class
Public Class HU
<XmlElement>
Public Property HU_X1 As String
<XmlElement>
Public Property HU_X2 As String
<XmlElement>
Public Property HU_X3 As String
' ...
End Class