Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to parse an XML file that contains an element
XML
<DateOut>2/11/2015</DateOut>

The date is arbitrary and changes per parent node.
XML
<?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...
VB
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
Posted
Comments
Maciej Los 15-Apr-15 14:04pm    
Do you want to delete jobs where the job has been completed 30 days ago?

1 solution

See an example:

VB
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

VB
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!
 
Share this answer
 
v2
Comments
Sean Donnahoe 15-Apr-15 14:49pm    
That's awesome. Thanks. On thing though, It doesn't show how to remove the entire Job node and re-save the XML.
Maciej Los 15-Apr-15 14:52pm    
Yes, it does remove nothing, because you didn't answer my question (in comment to your question). So, do you want to delete older than 30 days ago completed jobs?
Sean Donnahoe 15-Apr-15 15:04pm    
Sorry, I totally missed that. Yes, any job older than 30 days should be removed from the XML file.
Maciej Los 15-Apr-15 15:06pm    
Well, see updated answer ;)
Maciej Los 15-Apr-15 15:37pm    
If this answer meets your needs, please accept it (green button) - formally to remove your question from unanswered list...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900