比较Linq2XML中的日期 [英] Compare dates in Linq2XML
问题描述
我正在尝试解析包含元素的XML文件
< DateOut > 2015年2月11日< < span class =code-leadattribute> / DateOut >
日期是任意的和每个父节点的更改。
<? xml version = < span class =code-keyword> 1.0 encoding = utf-8 ? >
< CurrentJobs >
< < span class =code-leadattribute>作业 >
< JobID > 00d5dafa-f1d4-45e0-9744-904162428cab < / JobID >
< 状态 > a-current < /状态 >
< 客户名称 > Tennessee Valley Towing < / Custo merName >
< PurchaseOrder > TVT < / PurchaseOrder >
< 艺术品 > images \ TVT.png < / Artwork >
< DateIn > 2015年3月25日< / DateIn >
< DateOut > 4 / 21/2015 < / DateOut >
< Rush > 1 < / Rush >
< 详细信息 > White Grommet Flag < / Details >
< 注释 > < / Notes >
< Color1F > PANTONE 1585 C < / Color1F >
< Color2F > < / Color2F >
< Color3F > < / Color3F >
< Color4F > < / Color4F >
< Color5F > < < span class =code-leadattribute> / Color5F >
< Color6F > < / Color6F >
< Color1B > < / Color1B >
< < span class =code-leadattribute> Color2B > < / Color2B >
< Color3B > < / Color3B >
< Color4B > < / Color4B >
< Color5B > < / Color5B >
< Color6B > < / Color6B & gt;
< 计数 > 40 < / Count >
< 示例 > < /示例 > ;
< 已采取 > Leigh < / Taken > ;
< / Job >
< 作业 >
< JobID < span class =code-keyword>> 010eb5b4-926c-4bef-b1e6-f4df0f55d899 < / JobID >
< 状态 > b-complete < /状态 >
< 客户名称 > St Michael < / CustomerName >
< PurchaseOrder > 2014 Book Bee < / PurchaseOrder >
< 作品 > images \ 201414 Book Bee.png < / Artwork >
< DateIn > 2015年2月23日< / DateIn >
< DateOut < span class =code-keyword>> 2015年2月27日< / DateOut >
< Rush > 1 < ; / Rush >
< 详细信息 > Sport Gray T < / Details > ...
我想将DateOut元素与'今天'日期进行比较,如果DateOut是> 30天的时间跨度,我想删除整个父节点< job> ;.
我还想删除< artwork>中定义的PNG文件元素。
如果我使用了错误的术语(元素与节点),我很抱歉,我仍然不熟悉XML。
我已经开始使用以下代码...
公开 Sub CheckOldDates()
' load xml
Dim o As ObjectDataProvider
Dim xele 作为 XElement = XElement.Load( \\ARTSTATION\Users\Public\XML Job Board \current_jobs.xml)
' 检查已完成的工作
Dim completed =(来自job 在 xele.Desc中endants( Job)
其中job.Element( 状态)。值= b-complete _
选择作业)' 。SelectMany'.SingleOrDefault()
' 如果超过30天完成,删除
Dim ts As TimeSpan =今天 - Convert.ToDateTime(completed.Element( DateOut)。值)
MsgBox(ts.TotalDays)
' 删除图片png
' save xml
' xe.Save( \\ARTSTATION\Users\Public\XML Job Board \current_jobs.xml)
' o = FindResource(jobs)
' o.Refresh()
结束 Sub
查看示例:
Dim sFileName as String = EnterFullFileNameHere.xml
' 加载文档
Dim xDoc = XDocument.Load(sFileNa me)
' 获取日期=今天 - 30天
Dim mDate 作为 日期 = DateTime.Today ()。Addday(-30)
' 获取要删除的图片
< span class =code-keyword> Dim picturesToDelete =来自作业 xDoc.Root.Descendants( 作业)_
其中(job.Elements( 状态)。值= b-complete 和 job.Elements( DateOut ).Value< mDate)_
选择 job.Elements( 艺术品)。价值
' 循环播放图片..
对于 每个 p picturesToDelete
' 显示图片名称
Console.WriteLine( {0},p)
' < span class =code-comment> ToDo:在此处按名称删除图片
下一页
基于相同的逻辑,您可以删除作业
Dim jobsToDelete =来自作业 in xDoc.Root.Descendants( 作业)_
其中(job.Elements( Status)。Value = b-complete 和 job.Elements( DateOut)。值< mDate)_
选择作业
jobsToDelete.Remove()
xDoc.Save( NewOrOldFileName.xml)
祝你好运!
I am trying to parse an XML file that contains an element
<DateOut>2/11/2015</DateOut>
The date is arbitrary and changes per parent node.
<?xml version="1.0" encoding="utf-8"?>
<CurrentJobs>
<Job>
<JobID>00d5dafa-f1d4-45e0-9744-904162428cab</JobID>
<Status>a-current</Status>
<CustomerName>Tennessee Valley Towing</CustomerName>
<PurchaseOrder>TVT</PurchaseOrder>
<Artwork>images\TVT.png</Artwork>
<DateIn>3/25/2015 </DateIn>
<DateOut>4/21/2015 </DateOut>
<Rush>1</Rush>
<Details>White Grommet Flag</Details>
<Notes></Notes>
<Color1F>PANTONE 1585 C</Color1F>
<Color2F></Color2F>
<Color3F></Color3F>
<Color4F></Color4F>
<Color5F></Color5F>
<Color6F></Color6F>
<Color1B></Color1B>
<Color2B></Color2B>
<Color3B></Color3B>
<Color4B></Color4B>
<Color5B></Color5B>
<Color6B></Color6B>
<Count>40</Count>
<Sample></Sample>
<Taken>Leigh</Taken>
</Job>
<Job>
<JobID>010eb5b4-926c-4bef-b1e6-f4df0f55d899</JobID>
<Status>b-complete</Status>
<CustomerName>St Michael</CustomerName>
<PurchaseOrder>2014 Book Bee</PurchaseOrder>
<Artwork>images\2014 Book Bee.png</Artwork>
<DateIn>2/23/2015 </DateIn>
<DateOut>2/27/2015 </DateOut>
<Rush>1</Rush>
<Details>Sport Grey T</Details>...
I want to compare the DateOut element to the 'Today' date and if the DateOut is > 30 day timespan, I want to remove the entire parent node <job>.
I also want to deleted the PNG file defined in <artwork> element.
I apologize if I'm using the wrong terms (element vs. node), I'm still ind of new to XML.
I've gotten a start with the code below...
Public Sub CheckOldDates()
'load xml
Dim o As ObjectDataProvider
Dim xele As XElement = XElement.Load("\\ARTSTATION\Users\Public\XML Job Board\current_jobs.xml")
'check for completed job
Dim completed = (From job In xele.Descendants("Job")
Where job.Element("Status").Value = "b-complete" _
Select job) '.SelectMany '.SingleOrDefault()
'if older than 30 days completed, delete
Dim ts As TimeSpan = Today - Convert.ToDateTime(completed.Element("DateOut").Value)
MsgBox(ts.TotalDays)
'Delete image png
'save xml
'xe.Save("\\ARTSTATION\Users\Public\XML Job Board\current_jobs.xml")
'o = FindResource("jobs")
'o.Refresh()
End Sub
See an example:
Dim sFileName as String = "EnterFullFileNameHere.xml" 'load document Dim xDoc = XDocument.Load(sFileName) 'get date = Today - 30 days Dim mDate As Date = DateTime.Today().AddDays(-30) 'get pictures to delete Dim picturesToDelete = From job in xDoc.Root.Descendants("Job") _ Where (job.Elements("Status").Value = "b-complete" And job.Elements("DateOut").Value < mDate) _ Select job.Elements("Artwork").Value 'loop through the pictures.. For Each p in picturesToDelete 'display picture name Console.WriteLine("{0}", p) 'ToDo: delete picture by its name here Next
[EDIT]
Based on the same logic you can get jobs to delete
Dim jobsToDelete = From job in xDoc.Root.Descendants("Job") _ Where (job.Elements("Status").Value = "b-complete" And job.Elements("DateOut").Value < mDate) _ Select job jobsToDelete.Remove() xDoc.Save("NewOrOldFileName.xml")
Good luck!
这篇关于比较Linq2XML中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!