Solved

Insert XElement in right place in XDocument

Posted on 2013-06-10
2
269 Views
Last Modified: 2013-06-13
I have inherited following code. It keeps list of ManualInputs in an XML file a bit like this.  Code to add new items is as follows.  The problem with this is it always adds the new element to end of document. Instead I'd like to keep it in order of timestamp.  You can see here that 2011-01-10 was added and put at end, instead I want it to be added and keep order of timestamps ascending.
Thanks in advance for help

Dim manualInputsDoc as XDocument = GetTheXDocument
manualInputsDoc.Root.Add(element)
               
<?xml version="1.0" encoding="utf-8"?>
<ManualInputs>
  <ManualInput Timestamp="2011-01-01">
    <Field1>100</Field1>
  </ManualInput>
  <ManualInput Timestamp="2011-03-01">
    <Field1>200</Field1>
  </ManualInput>
  <ManualInput Timestamp="2011-05-01">
    <Field1>123</Field1>
  </ManualInput>
  <ManualInput Timestamp="2011-01-10">
    <Field1>444</Field1>
  </ManualInput>
</ManualInputs>
0
Comment
Question by:rwallacej
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39234264
this code reordered the xml by timestamp after u added the new one.
        
Dim manualInputsDoc as XDocument = GetTheXDocument
manualInputsDoc.Root.Add(element)
Dim ordered = manualInputsDoc.Root.Elements().OrderBy(Function(n) DateTime.ParseExact(n.Attribute("Timestamp").Value, "yyyy-MM-dd", CultureInfo.CurrentCulture)).ToArray()
        manualInputsDoc.Root.RemoveAll()
        manualInputsDoc.Root.Add(ordered)

Open in new window

0
 

Author Closing Comment

by:rwallacej
ID: 39245615
Thanks for help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now