我试图修改答案中给出的代码,从xml文件中提取比原先输入的更多信息但是我得到了一些错误
[代码]
Imports System.Xml
Imports System.Xml.Linq
Imports System.IO
Public Class Form1
Public FILENAME As String = String.Empty
Public models As Model
Public productionBlocks As New Panel
Public pictureBlocks As New List(Of PictureBox)
Public gridsinfo As CubeGrid
Public dictCount As New Dictionary(Of String, Integer)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
AddHandler Me.Load, AddressOf Form1_Load
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
Dim fileloader As New OpenFileDialog
fileloader.ShowDialog()
FILENAME = fileloader.FileName
models = New Model
models.Load(FILENAME)
Dim blockNames As List(Of String) = models.print.Select(Function(x) x.cubes.Select(Function(y) y.cubeBlocks.Select(Function(z) z.SubtypeName)).SelectMany(Function(y) y).ToList()).FirstOrDefault()
Dim gridSizeEnumerator As String = models.print.Select(Function(x) gridsinfo.gridSizeEnum).ToString
dictCount = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Count)
Dim display As String = models.print2.Select(Function(x) x.cubes.Select(Function(y) y.displayname)).SelectMany(Function(y) y).ToString().First()
Dim enumer As String = models.print3.Select(Function(x) x.cubes.Select(Function(y) y.enumerator)).SelectMany(Function(y) y).ToString().First()
For Each item In dictCount
InfluenceListBox1.AddItem(item)
Next item
For Each key As String In dictCount.Keys.AsEnumerable()
TextBox1.Text = "subTypeName = '{0}', count = '{1}'" + key.ToString + dictCount(key).ToString + Environment.NewLine
Next key
If InfluenceListBox1.lstbox.Items.Item(1).ToString.Contains("Large") Then
InfluenceTextBox1.Text = "Grid Size: Large Ship"
ElseIf InfluenceListBox1.lstbox.Items.Item(1).ToString.Contains("Small") Then
InfluenceTextBox1.Text = "Grid Size: Small Ship"
End If
InfluenceTextBox2.Text = "Grid Owner: " + display.ToString
InfluenceTextBox3.Text = "Grid Size Enumerator: " + enumer.ToString
End Sub
End Class
Public Class Model
Public print As New List(Of Model)
Public print2 As New List(Of Model)
Public print3 As New List(Of Model)
Public _type As String
Public _id As ID
Public _display As String
Public _display1 As String
Public displayname As String
Public enumerator As String
Public cubes As List(Of CubeGrid)
Public info As String
Public Sub Load(filename As String)
Dim reader As New StreamReader(filename)
Dim doc As XDocument = XDocument.Load(reader)
Dim firstNode As XElement = doc.FirstNode
Dim xsiNs = firstNode.GetNamespaceOfPrefix("xsi")
Dim drawingTypes = firstNode.Elements.FirstOrDefault()
Dim drawingType = drawingTypes.Elements.FirstOrDefault()
Dim drawingStr = drawingType.Name.LocalName
print = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
._type = x.Attribute(xsiNs + "type"), ._id = x.Elements("Id").Select(Function(y) New ID() With {
.type = y.Attribute("Type"),
.subtype = y.Attribute("Subtype")
}).FirstOrDefault(),
.cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
.id = y.Element("EntityId"),
.persistentFlags = y.Element("PersistentFlags"),
.position = y.Descendants("Position").Select(Function(z) New location() With {
.x = CType(z.Attribute("x"), Double),
.y = CType(z.Attribute("y"), Double),
.z = CType(z.Attribute("z"), Double)
}).FirstOrDefault(),
.forward = y.Descendants("Forward").Select(Function(z) New location() With {
.x = CType(z.Attribute("x"), Double),
.y = CType(z.Attribute("y"), Double),
.z = CType(z.Attribute("z"), Double)
}).FirstOrDefault(),
.up = y.Descendants("Up").Select(Function(z) New location() With {
.x = CType(z.Attribute("x"), Double),
.y = CType(z.Attribute("y"), Double),
.z = CType(z.Attribute("z"), Double)
}).FirstOrDefault(),
.orientation = y.Descendants("Orientation").Select(Function(z) New location() With {
.w = CType(z.Element("W"), Double),
.x = CType(z.Element("X"), Double),
.y = CType(z.Element("Y"), Double),
.z = CType(z.Element("Z"), Double)
}).FirstOrDefault(),
.cubeBlocks = y.Descendants("MyObjectBuilder_CubeBlock").Select(Function(z) New CubeBlock() With {
.SubtypeName = z.Element("SubtypeName")
}).ToList()
}).ToList()
}).ToList()
print2 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
.cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
.displayname = y.Element("DisplayName")
}).ToList()
}).ToList()
print3 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
.cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
.enumerator = y.Element("GridSizeEnum")
}).ToList()
}).ToList()
End Sub
End Class
Public Class ID
Public type As String
Public subtype As String
End Class
Public Class CubeGrid
Public id As String
Public persistentFlags As String
Public position As location
Public forward As location
Public up As location
Public orientation As location
Public cubeBlocks As List(Of CubeBlock)
Public displayname As String
Public enumerator As String
End Class
Public Class location
Public w As Double
Public x As Double
Public y As Double
Public z As Double
End Class
Public Class CubeBlock
Public SubtypeName As String
Public username As String
End Class
[/代码]
错误是将标签文本转换为显示名称时从字符串中抛出无效的转换异常我还想添加更多项目,例如网格名称和网格大小枚举器,这样我就可以删除ifcontains()方法列表框任何帮助将不胜感激!
答案 0 :(得分:0)
尝试此作为开始
Imports System.Xml
Imports System.Xml.Linq
Imports System.IO
Public Class Form1
Const FILENAME As String = "c:\temp\test.xml"
Public models As Model
Public productionBlocks As New Panel
Public pictureBlocks As New List(Of PictureBox)
Public dictCount As New Dictionary(Of String, Integer)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
'AddHandler Me.Load, AddressOf Form1_Load
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
models = New Model
models.Load(FILENAME)
Dim blockNames As List(Of String) = models.print.Select(Function(x) x.cubes.Select(Function(y) y.cubeBlocks.Select(Function(z) z.SubtypeName)).SelectMany(Function(y) y).ToList()).FirstOrDefault()
dictCount = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Count)
End Sub
End Class
Public Class Model
Public print As New List(Of Model)
Public _type As String
Public _id As ID
Public cubes As List(Of CubeGrid)
Public Sub Load(filename As String)
Dim reader As New StreamReader(filename)
Dim doc As XDocument = XDocument.Load(reader)
Dim firstNode As XElement = doc.FirstNode
Dim xsiNs = firstNode.GetNamespaceOfPrefix("xsi")
Dim drawingTypes = firstNode.Elements.FirstOrDefault()
Dim drawingType = drawingTypes.Elements.FirstOrDefault()
Dim drawingStr = drawingType.Name.LocalName
print = doc.Descendants(drawingStr).Select(Function(x) New Model() With { _
._type = x.Attribute(xsiNs + "type"), _
._id = x.Elements("Id").Select(Function(y) New ID() With { _
.type = y.Attribute("Type"), _
.subtype = y.Attribute("Subtype") _
}).FirstOrDefault(), _
.cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With { _
.id = y.Element("EntityId"),
.persistentFlags = y.Element("PersistentFlags"),
.gridSizeEnum = y.Element("GridSizeEnum"),
.position = y.Descendants("Position").Select(Function(z) New location() With { _
.x = CType(z.Attribute("x"), Double), _
.y = CType(z.Attribute("y"), Double), _
.z = CType(z.Attribute("z"), Double) _
}).FirstOrDefault(), _
.forward = y.Descendants("Forward").Select(Function(z) New location() With { _
.x = CType(z.Attribute("x"), Double), _
.y = CType(z.Attribute("y"), Double), _
.z = CType(z.Attribute("z"), Double) _
}).FirstOrDefault(), _
.up = y.Descendants("Up").Select(Function(z) New location() With { _
.x = CType(z.Attribute("x"), Double), _
.y = CType(z.Attribute("y"), Double), _
.z = CType(z.Attribute("z"), Double) _
}).FirstOrDefault(),
.orientation = y.Descendants("Orientation").Select(Function(z) New location() With { _
.w = CType(z.Element("W"), Double), _
.x = CType(z.Element("X"), Double), _
.y = CType(z.Element("Y"), Double), _
.z = CType(z.Element("Z"), Double) _
}).FirstOrDefault(), _
.cubeBlocks = y.Descendants("MyObjectBuilder_CubeBlock").GroupBy(Function(z) CType(z.Element("SubtypeName"), String)).Select(Function(z) New CubeBlock() With { _
.SubtypeName = z.Key,
.count = z.Count
}).ToList()
}).ToList()
}).ToList()
End Sub
End Class
Public Class ID
Public type As String
Public subtype As String
End Class
Public Class CubeGrid
Public id As String
Public persistentFlags As String
Public gridSizeEnum As String
Public position As location
Public forward As location
Public up As location
Public orientation As location
Public cubeBlocks As List(Of CubeBlock)
End Class
Public Class location
Public w As Double
Public x As Double
Public y As Double
Public z As Double
End Class
Public Class CubeBlock
Public SubtypeName As String
Public count As Integer
End Class
问题解答
1)
现在我该如何使用这个' dictCount
For Each key As String In dictCount.Keys.AsEnumerable()
Console.WriteLine("subTypeName = '{0}', count = '{1}'", key, dictCount(key))
Next key
2)我做了什么?
Dim print2 = doc.Descendants(drawingStr).Select(Function(x) New Model() With { _
.cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With { _
.displayname = y.Element("DisplayName") _
}).ToList() _
}).ToList()