Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert XElement in right place in XDocument

Posted on 2013-06-10
2
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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