Solved

Insert XElement in right place in XDocument

Posted on 2013-06-10
2
268 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
Comment Utility
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
Comment Utility
Thanks for help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

11 Experts available now in Live!

Get 1:1 Help Now