?
Solved

Insert XElement in right place in XDocument

Posted on 2013-06-10
2
Medium Priority
?
280 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

800 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