Solved

Insert XElement in right place in XDocument

Posted on 2013-06-10
2
275 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:
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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