在VB.NET / C#

时间:2018-04-30 16:53:57

标签: xml vb.net serialization

我正试图通过此端点从欧洲中央银行(ECB)获取数据:https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

我制作了一个适合XML元素的结构。但是,子节点没有出现。我使用了错误的元素吗?这是结构。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
  <gesmes:subject>Reference rates</gesmes:subject>
  <gesmes:Sender>
    <gesmes:name>European Central Bank</gesmes:name>
  </gesmes:Sender>
  <Cube>
    <Cube time="2018-04-30">
      <Cube currency="USD" rate="1.2079"/>
      <Cube currency="JPY" rate="132.12"/>
      <Cube currency="BGN" rate="1.9558"/>
      <Cube currency="CZK" rate="25.542"/>
      <Cube currency="DKK" rate="7.4501"/>
      <Cube currency="GBP" rate="0.87960"/>
      <Cube currency="HUF" rate="313.55"/>
      <Cube currency="PLN" rate="4.2264"/>
      <Cube currency="RON" rate="4.6614"/>
      <Cube currency="SEK" rate="10.4993"/>
      <Cube currency="CHF" rate="1.1968"/>
      <Cube currency="ISK" rate="122.20"/>
      <Cube currency="NOK" rate="9.6620"/>
      <Cube currency="HRK" rate="7.4100"/>
      <Cube currency="RUB" rate="75.9587"/>
      <Cube currency="TRY" rate="4.8896"/>
      <Cube currency="AUD" rate="1.6013"/>
      <Cube currency="BRL" rate="4.1932"/>
      <Cube currency="CAD" rate="1.5542"/>
      <Cube currency="CNY" rate="7.6574"/>
      <Cube currency="HKD" rate="9.4801"/>
      <Cube currency="IDR" rate="16796.15"/>
      <Cube currency="ILS" rate="4.3378"/>
      <Cube currency="INR" rate="80.1685"/>
      <Cube currency="KRW" rate="1292.04"/>
      <Cube currency="MXN" rate="22.5977"/>
      <Cube currency="MYR" rate="4.7409"/>
      <Cube currency="NZD" rate="1.7145"/>
      <Cube currency="PHP" rate="62.452"/>
      <Cube currency="SGD" rate="1.6016"/>
      <Cube currency="THB" rate="38.145"/>
      <Cube currency="ZAR" rate="15.0121"/>
    </Cube>
  </Cube>
</gesmes:Envelope>

类:

<XmlRootAttribute([ElementName]:="Sender", [Namespace]:="http://www.gesmes.org/xml/2002-08-01")>
Public Class Sender
    <XmlElement([ElementName]:="name", [Namespace]:="http://www.gesmes.org/xml/2002-08-01")>
    Public Property Name As String
End Class

<XmlRootAttribute([ElementName]:="Cube", [Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref")>
Public Class Cube
    <XmlAttribute([AttributeName]:="name")>
    Public Property Currency As String

    <XmlAttribute([AttributeName]:="rate")>
    Public Property Rate As String
End Class

<XmlRootAttribute([ElementName]:="Envelope", [Namespace]:="http://www.gesmes.org/xml/2002-08-01")>
Public Class Envelope
    <XmlElement([ElementName]:="subject", [Namespace]:="http://www.gesmes.org/xml/2002-08-01")>
    Public Property subject As String

    <XmlElement([ElementName]:="Sender", [Namespace]:="http://www.gesmes.org/xml/2002-08-01")>
    Public Property Sender As Sender

    <XmlElement([ElementName]:="Cube", [Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref")>
    Public Property Cube As Cube

    <XmlAttribute([AttributeName]:="gesmes", [Namespace]:="http://www.w3.org/2000/xmlns/")>
    Public Property Gesmes As String

    <XmlAttribute([AttributeName]:="xmlns")>
    Public Property Xmlns As String
End Class

当我采用这种结构并使用以下函数将其序列化时

Public Function ToXML(ByVal struct As Envelope, ByVal t As Type) As String
    Dim stringwriter = New System.IO.StringWriter()
    Dim serializer = New XmlSerializer(t)
    serializer.Serialize(stringwriter, struct)
    Return stringwriter.ToString()
End Function

删除所有子节点。

1 个答案:

答案 0 :(得分:0)

使用这些类

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://www.gesmes.org/xml/2002-08-01"),
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://www.gesmes.org/xml/2002-08-01", IsNullable:=False)>
Partial Public Class Envelope
    Public Property subject() As String
    Public Property Sender() As EnvelopeSender
    <System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref")>
    Public Property Cube() As Cube
End Class

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://www.gesmes.org/xml/2002-08-01")>
Partial Public Class EnvelopeSender
    Public Property name() As String
End Class

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"),
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref", IsNullable:=False)>
Partial Public Class Cube
    <System.Xml.Serialization.XmlElementAttribute("Cube")>
    Public Property Cube1() As CubeCube
End Class

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref")>
Partial Public Class CubeCube
    <System.Xml.Serialization.XmlElementAttribute("Cube")>
    Public Property Cube() As CubeCubeCube()
    <System.Xml.Serialization.XmlAttributeAttribute(DataType:="date")>
    Public Property time() As Date
End Class

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://www.ecb.int/vocabulary/2002-08-01/eurofxref")>
Partial Public Class CubeCubeCube
    <System.Xml.Serialization.XmlAttributeAttribute()>
    Public Property currency() As String
    <System.Xml.Serialization.XmlAttributeAttribute()>
    Public Property rate() As Decimal
End Class

用法:

Dim xml = <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
                <gesmes:subject>Reference rates</gesmes:subject>
                <gesmes:Sender>
                    <gesmes:name>European Central Bank</gesmes:name>
                </gesmes:Sender>
                <Cube>
                    <Cube time="2018-04-30">
                        <Cube currency="USD" rate="1.2079"/>
                        <Cube currency="JPY" rate="132.12"/>
                        <Cube currency="BGN" rate="1.9558"/>
                    </Cube>
                </Cube>
            </gesmes:Envelope>
Dim s As New Xml.Serialization.XmlSerializer(GetType(Envelope))
Dim env As Envelope
' instead of xml.ToString here, you could use a streamreader for example
Using sr As New System.IO.StringReader(xml.ToString)
    env = CType(s.Deserialize(sr), Envelope)
End Using