我的planets.xml中的位置没有输入正确格式的xml错误输入流,没有任何错误。 Visual Studio 2017 4.7框架。 WCF已安装。 link(IDE中的错误)使用streamreader加载xml,它给出了位置19,10的错误,即<子类型字段。 link(在planets.xml内)刚刚用xmlreader尝试了这个,新的错误是位置18,35,它是光谱和类(单字)之间的结束标记。我迷失了,在这里和网上的其他门票通常你可以立即看到他们的xml文件中的错误,但没有任何东西跳出来为我的问题。
Planets.xml(短段,3,146个行星)
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<planets>
<planet>
<name>A Place</name>
<xcood>-123.764</xcood>
<ycood>272.182</ycood>
</planet>
<planet>
<name>Abadan</name>
<xcood>-70.319</xcood>
<ycood>-95.949</ycood>
<pressure>3</pressure>
<gravity>1.03</gravity>
<lifeForm>7</lifeForm>
<climate>2</climate>
<percentWater>63</percentWater>
<temperature>22</temperature>
<spectralClass>F</spectralClass>
<subtype>1</subtype>
<luminosity>V</luminosity>
<sysPos>5</sysPos>
<socioIndustrial>C-C-D-C-C</socioIndustrial>
<landMass>Abadan Major (Abbasid)</landMass>
<landMass>Abadan Minor</landMass>
<landMass>Kuran Major</landMass>
<landMass>Kuran Minor</landMass>
<hpg>B</hpg>
</planet>
</planets>
Planets.vb
'''<remarks/>
<System.SerializableAttribute(),
System.ComponentModel.DesignerCategoryAttribute("code"),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True),
System.Xml.Serialization.XmlRootAttribute("planets", IsNullable:=False)>
Partial Public Class Planets
Private planetField() As planetsPlanet
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("planet")>
Public Property planet() As planetsPlanet()
Get
Return Me.planetField
End Get
Set
Me.planetField = Value
End Set
End Property
End Class
'''<remarks/>
<System.SerializableAttribute(),
System.ComponentModel.DesignerCategoryAttribute("code"),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)>
Partial Public Class planetsPlanet
Private n As String
Private x As Decimal
Private y As Decimal
Private a As String
Private o As String
Private f As String
Private fC As planetsPlanetFactionChange
Private p As Integer
Private g As Decimal
Private nC As Boolean
Private zC As Boolean
Private lF As Integer
Private c As Integer
Private pW As Integer
Private t As Integer
Private sC As Char
Private sT As Integer
Private l As Char
Private sP As Integer
Private sI As String
Private s As String
Private lM As String
Private HPG As Char
Private d As String
<System.Xml.Serialization.XmlElementAttribute("name", GetType(String))>
Public Property name() As String
Get
Return Me.n
End Get
Set(value As String)
Me.n = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("xcood", GetType(Decimal))>
Public Property xcood() As Decimal
Get
Return Me.x
End Get
Set(value As Decimal)
Me.x = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("ycood", GetType(Decimal))>
Public Property ycood() As Decimal
Get
Return Me.y
End Get
Set(value As Decimal)
Me.y = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("axis", GetType(String))>
Public Property axis() As String
Get
Return Me.a
End Get
Set(value As String)
Me.a = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("orbit", GetType(String))>
Public Property orbit() As String
Get
Return Me.o
End Get
Set(value As String)
Me.o = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("faction", GetType(String))>
Public Property faction() As String
Get
Return Me.f
End Get
Set(value As String)
Me.f = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("factionChange", GetType(planetsPlanetFactionChange))>
Public Property factionChange() As planetsPlanetFactionChange
Get
Return Me.fC
End Get
Set(value As planetsPlanetFactionChange)
Me.fC = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("pressure", GetType(Integer))>
Public Property pressure() As Integer
Get
Return Me.p
End Get
Set(value As Integer)
Me.p = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("gravity", GetType(Decimal))>
Public Property gravity() As Decimal
Get
Return Me.g
End Get
Set(value As Decimal)
Me.g = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("nadirCharge", GetType(Boolean))>
Public Property nadirCharge() As Boolean
Get
Return Me.nC
End Get
Set(value As Boolean)
Me.nC = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("zenithCharge", GetType(Boolean))>
Public Property zenithCharge() As Boolean
Get
Return Me.zC
End Get
Set(value As Boolean)
Me.zC = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("lifeForm", GetType(Integer))>
Public Property lifeForm() As Integer
Get
Return Me.lF
End Get
Set(value As Integer)
Me.lF = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("climate", GetType(Integer))>
Public Property climate() As Integer
Get
Return Me.c
End Get
Set(value As Integer)
Me.c = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("percentWater", GetType(Integer))>
Public Property percentWater() As Integer
Get
Return Me.pW
End Get
Set(value As Integer)
Me.pW = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("temperature", GetType(Integer))>
Public Property temperature() As Integer
Get
Return Me.t
End Get
Set(value As Integer)
Me.t = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("spectralClass", GetType(Char))>
Public Property spectralClass() As Char
Get
Return Me.sC
End Get
Set(value As Char)
Me.sC = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("subtype", GetType(Integer))>
Public Property subtype() As Integer
Get
Return Me.sT
End Get
Set(value As Integer)
Me.sT = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("luminosity", GetType(Char))>
Public Property luminosity() As Char
Get
Return Me.l
End Get
Set(value As Char)
Me.l = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("sysPos", GetType(Integer))>
Public Property sysPos() As Integer
Get
Return Me.sP
End Get
Set(value As Integer)
Me.sP = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("socioIndustrial", GetType(String))>
Public Property socioIndustrial() As String
Get
Return Me.sI
End Get
Set(value As String)
Me.sI = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("satellite", GetType(String))>
Public Property satellite() As String
Get
Return Me.s
End Get
Set(value As String)
Me.s = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("landMass", GetType(String))>
Public Property landMass() As String
Get
Return Me.lM
End Get
Set(value As String)
Me.lM = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("hpg", GetType(Char))>
Public Property HPGStation() As Char
Get
Return Me.HPG
End Get
Set(value As Char)
Me.HPG = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("desc", GetType(String))>
Public Property desc() As String
Get
Return Me.d
End Get
Set(value As String)
Me.d = value
End Set
End Property
End Class
'''<remarks/>
<System.SerializableAttribute(),
System.ComponentModel.DesignerCategoryAttribute("code"),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)>
Partial Public Class planetsPlanetFactionChange
Private dateField As String
Private factionField As String
'''<remarks/>
Public Property [date]() As String
Get
Return Me.dateField
End Get
Set
Me.dateField = Value
End Set
End Property
'''<remarks/>
Public Property faction() As String
Get
Return Me.factionField
End Get
Set
Me.factionField = Value
End Set
End Property
End Class
objPlanets.vb(我正在尝试序列化xml)
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Class objPlanets
Private Sub serialPlanets()
Dim serial As New XmlSerializer(GetType(Planets))
Dim p As Planets
Dim reader As XmlReader = XmlReader.Create(My.Application.Info.DirectoryPath & "\Planets\planets.xml")
Do While reader.Read()
p = serial.Deserialize(reader)
Loop
reader.Close()
For Each planet In p.planet()
If planet.axis() = String.Empty Then
planet.axis = getAxis()
Else
Continue For
End If
Next
Using fs As New FileStream(My.Application.Info.DirectoryPath & "\Planets\planets.xml", FileMode.Truncate)
serial.Serialize(fs, p)
End Using
End Sub
Private Function getAxis() As String
Select Case roll2D6()
Case 2 To 6
Return "Tilted"
Case 7 To 12
Return "No Tilt"
Case Else
Return "Error"
End Select
End Function
Private Function getOrbit() As String
Select Case roll2D6()
Case 2 To 6
Return "Elliptical"
Case 7 To 12
Return "Circular"
Case Else
Return "Error"
End Select
End Function
Private Function roll2D6() As Integer
Dim rollResult1 As Integer = CInt((Rnd() * 5) + 1)
Dim rollResult2 As Integer = CInt((Rnd() * 5) + 1)
Return rollResult1 + rollResult2
End Function
Public Sub New()
serialPlanets()
End Sub
End Class
答案 0 :(得分:1)
好的,在重新输入错误位置行然后注意到上面和下面的行后,我已经设置接受Char,我猜序列化不喜欢。将这些更改为字符串。
<System.Xml.Serialization.XmlElementAttribute("spectralClass", GetType(String))>
Public Property spectralClass() As String
<System.Xml.Serialization.XmlElementAttribute("subtype", GetType(Integer))>
Public Property subtype() As Integer
<System.Xml.Serialization.XmlElementAttribute("luminosity", GetType(String))>
Public Property luminosity() As String
然后开始给我新的错误位置,并发现无论谁进入各种行星的某些真实场地,有时会输入&#34; True,TRUE或TRUE&#34;并且它对那些因此必须将它们全部变为真实而感到厌恶。现在它很棒,为我写了一个更完整的planets.xml。非常感谢你们,我现在可以再次开始取得进展。
答案 1 :(得分:1)
您的问题是Char
类型属性。虽然您的xml数据在这些属性中只有一个字母,但它是String
。数据值必须是表示UTF-8字符的整数。
即。 <spectralClass>F</spectralClass>
应为<spectralClass>70</spectralClass>
将Char
类型的所有属性更改为String
类型可能会更容易。