使用 LINQ 一次添加多个 xElements to XML
Adding multiple xElements at once using LINQ to XML
我有以下自定义 class:
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 Literals, which allow you to embed expressions 从您的对象中构建您的 XML。对我来说,它似乎比所有嵌套 New XElement
更自然。例如
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)
但是,您关于 "not working, needs implements IQueryable" 的评论让我想知道您是否遗漏了某事的 reference/import ……或者我是否遗漏了什么。
我有以下自定义 class:
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 Literals, which allow you to embed expressions 从您的对象中构建您的 XML。对我来说,它似乎比所有嵌套 New XElement
更自然。例如
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)
但是,您关于 "not working, needs implements IQueryable" 的评论让我想知道您是否遗漏了某事的 reference/import ……或者我是否遗漏了什么。