gpt4 book ai didi

xml - 改进功能以使用标签从字符串填充对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:28:56 25 4
gpt4 key购买 nike

我想用我的字符串数据填充一个看起来像 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

最佳答案

如果你有带标签的字符串并且你想填充对象,请使用 XML 而不是 Regexp。

为此,请点击此链接:https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx

  • 将字符串转换为 XMLDocument
  • 将您的 XML 序列化为对象。

用于创建带有字符串和填充对象的 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

关于xml - 改进功能以使用标签从字符串填充对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40489758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com