如何将表示类的XML元素减少为单个字符串?我的XML序列化器输出:
<ArrayOfCity>
<City>
<Name>Los Angeles</Name>
<Location>
<Value>034.050N -118.250E</Value>
</Location>
<Elevation>305</Elevation>
<Population>3884000</Population>
</City>
<City>
<Name>New York</Name>
<Location>
<Value>040.713N -074.006E</Value>
</Location>
<Elevation>33</Elevation>
<Population>8550405</Population>
</City>
</ArrayOfCity>
但我希望它看起来像这样(简化<Location>
元素):
<ArrayOfCity>
<City>
<Name>Los Angeles</Name>
<Location>034.050N -118.250E</Location>
<Elevation>305</Elevation>
<Population>3884000</Population>
</City>
<City>
<Name>New York</Name>
<Location>040.713N -074.006E</Location>
<Elevation>33</Elevation>
<Population>8550405</Population>
</City>
</ArrayOfCity>
我的班级实施是:
Public Class City
Public Property Name As String
Public Property Location As LatLong
Public Property Elevation As Integer
Public Property Population As Integer
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Public Class LatLong
Implements ISerializable
<XmlIgnore()>
Private _Lat As Single
<XmlIgnore()>
Private _Long As Single
<XmlIgnore()>
Public Property Latitude As Single
Get
Return _Lat
End Get
Set(value As Single)
_Lat = value
End Set
End Property
<XmlIgnore()>
Public Property Longitude As Single
Get
Return _Long
End Get
Set(value As Single)
_Long = value
End Set
End Property
Public Property Value As String
Get
Return String.Format("{0:000.000}N {1:000.000}E", Latitude, Longitude)
End Get
Set(value As String)
'Pos Value: N & E, Neg Value: S & W
'Format must be [###.###N ###.###E] (spacing and digits can be variable, but cannot use S or W)
_Lat = CSng(Split(value, "N")(0))
_Long = CSng(Split(value.Replace("E", ""), "N")(1))
End Set
End Property
Public Sub New()
_Lat = 0
_Long = 0
End Sub
Public Sub New(InitialString As String)
Value = InitialString
End Sub
Public Overrides Function ToString() As String
Return Value
End Function
Public Shared Widening Operator CType(ByVal O As String) As LatLong
Return New LatLong(O)
End Operator
Public Shared Narrowing Operator CType(O As LatLong) As String
Return O.ToString
End Operator
Public Overrides Function Equals(obj As Object) As Boolean
With DirectCast(obj, LatLong)
Return Latitude = .Latitude AndAlso Longitude = .Longitude
End With
End Function
Public Shared Operator =(Left As LatLong, Right As LatLong) As Boolean
Return Left.Equals(Right)
End Operator
Public Shared Operator <>(Left As LatLong, Right As LatLong) As Boolean
Return Not Left.Equals(Right)
End Operator
Protected Sub New(info As SerializationInfo, context As StreamingContext)
'Value = DirectCast(info.GetValue("props", GetType(String)), String)
End Sub
Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
Throw New NotImplementedException()
End Sub
End Class
我很确定密钥会出现在Protected Sub New(...)
和Public Sub GetObjectData(...)
中,但我无法弄明白。如果可以确定的话,最好将它们用于XML元素属性。
感谢您的帮助!
更新: 我想到了。 LatLong类需要实现IXmlSerializable,WriteXML(...)和ReadXML(...)subs需要像这样实现:
Public Sub ReadXml(reader As XmlReader) Implements IXmlSerializable.ReadXml
Value = reader.ReadElementContentAsString
End Sub
Public Sub WriteXml(writer As XmlWriter) Implements IXmlSerializable.WriteXml
writer.WriteString(Value)
End Sub
答案 0 :(得分:0)
LatLong类需要实现IXmlSerializable,而WriteXML(...)和ReadXML(...)subs需要像这样实现:
Public Sub ReadXml(reader As XmlReader) Implements IXmlSerializable.ReadXml
Value = reader.ReadElementContentAsString
End Sub
Public Sub WriteXml(writer As XmlWriter) Implements IXmlSerializable.WriteXml
writer.WriteString(Value)
End Sub