我有以下自定义类:
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
我正在尝试将多个xElements一次添加到XML文档中,具体取决于是否存在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>
答案 0 :(得分:1)
我会使用XML Literals,它允许您embed expressions从对象构建XML。对我而言,这比所有嵌套的New XElement
更自然。 e.g。
Dim frameData = {
New Frame(Date.Now, "A", Frame.FrameResult.Success),
New Frame(Date.Now, "B", Frame.FrameResult.Success),
New Frame(Date.Now, "C", Frame.FrameResult.Failed),
New Frame(Date.Now, "D", Frame.FrameResult.Success),
New Frame(Date.Now, "E", Frame.FrameResult.Success),
New Frame(Date.Now, "F", Frame.FrameResult.Success)
}
Dim xml As XDocument = <?xml version="1.0" encoding="UTF-8"?><frames></frames>
Dim frames As IEnumerable(Of XElement) =
From f In frameData
Select <frame>
<timestamp><%= f.TimeStamp %></timestamp>
<content><%= f.Content %></content>
<status><%= If(f.Result = Frame.FrameResult.Success, "OK", "FAIL") %></status>
</frame>
xml.Root.Add(frames)
但是,你关于“不工作,需要实现IQueryable”的评论让我想知道你是否错过了参考/导入的东西......或者我是否遗漏了什么。