【发布时间】:2016-02-22 16:53:04
【问题描述】:
我有以下自定义类:
Public Class Frame
#Region " Enumerations "
Public Enum FrameResult
Success
Failed
End Enum
#End Region
#Region " Private Members "
Private _timeStamp As Date
Private _content As String
Private _result As String
#End Region
#Region " Constructors "
Sub New(ByVal timeStamp As Date, ByVal content As String, ByVal result As FrameResult)
Me._timeStamp = timeStamp
Me._content = content
Me._result = result.ToString
End Sub
#End Region
#Region " Properties "
Public Property TimeStamp() As Date
Get
Return Me._timeStamp
End Get
Set(ByVal value As Date)
Me._timeStamp = value
End Set
End Property
Public Property Content() As String
Get
Return Me._content
End Get
Set(ByVal value As String)
Me._content = value
End Set
End Property
Public Property Result() As FrameResult
Get
Return DirectCast([Enum].Parse(GetType(FrameResult), Me._result), FrameResult)
End Get
Set(ByVal value As FrameResult)
Me._result = value.ToString
End Set
End Property
#End Region
End Class
我正在尝试根据 xml 文件是否存在,一次将多个 xElements 添加到 XML 文档中:
Public Sub Add(ByVal frameData() as Frame) {
if FileExists(xmlFile) then
' XML File exists
xElem = XElement.Load(xmlFile)
xElem.Add(frameData) --> not working
' Persist changes to file. Save to Disk
xElem.Save(xmlFile)
else
' File does not exist
xDoc = New XDocument(New XDeclaration("1.0", "UTF-8", Nothing))
xDoc.Root.Add(frameData)
sw = New StringWriter()
xWrite = XmlWriter.Create(sw)
xDoc.Save(xWrite)
xWrite.Close()
' Persist changes to file. Save to Disk
xDoc.Save(xmlFile)
End If
但它不起作用。我已经尝试过:
Dim _frame as Frame
xDoc = New XDocument(New XDeclaration("1.0", "UTF-8", Nothing), _
new XElement("Frames", _
from _frame in frameData <--- not working, needs implements IQueryable
select new XElement("TimeStamp", _frame.TimeStamp), _
new XElement("Content", _frame.Content), _
new XElement("Result", _frame.Result)))
但它不起作用,因为 frameData 没有实现 IQueryable 并且看起来很困难......还有其他简单的方法吗?
我的 xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<frames>
<frame>
<timestamp>20/10/2016 07:30:05 AM</timestamp>
<content>bla bla bla </content>
<status>OK</status>
</frame>
<frame>
<timestamp>20/10/2016 15:10:12 PM</timestamp>
<content>bla bla bla </content>
<status>FAIL</status>
</frame>
... NEXT FRAMES ...
</frames>
【问题讨论】:
标签: xml vb.net linq linq-to-xml xelement