Solved

sorting XML

Posted on 2013-11-15
4
255 Views
Last Modified: 2013-11-30
Hi,

I would like to read the simple XML below.
<list version="1.0">
<meta>
  <type>resource-list</type>
</meta>
<resources start="0" count="4">
  <resource classname="Quote">
    <field name="name">Vincent</field>
    <field name="username">Hill</field>
    <field name="age">0.32</field>
    <field name="hair">black</field>
  </resource>
  <resource classname="Quote">
    <field name="name">John</field>
    <field name="username">Tedelon</field>
    <field name="age">27</field>
    <field name="hair">brown</field>
  </resource>
  <resource classname="Quote">
    <field name="name">Michael</field>
    <field name="username">Lopez</field>
    <field name="age">20</field>
    <field name="hair">red</field>
  </resource>
  <resource classname="Quote">
    <field name="name">Frank</field>
    <field name="username">Lopez</field>
    <field name="age">25</field>
    <field name="hair">black</field>
  </resource>
</resources>
</list>

Open in new window


 and be able to find the record and the corresponding name with the max age.

How best can it be done with the minimum lines of code?

thanks!
0
Comment
Question by:olmuser
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39653356
One possible approach would be load the XML into a System.Data.DataSet, and then use a filter to find the record that you need.
0
 
LVL 1

Author Comment

by:olmuser
ID: 39653416
Thanks @TheLearnedOne, can you show me the code to do that for the above XML?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39653455
The appropriate method depends on where you are getting the XML.

Here is an example:

Loading a DataSet from XML
http://msdn.microsoft.com/en-us/library/fx29c3yd(v=vs.110).aspx

DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);

string xmlData = "<XmlDS><table1><col1>Value1</col1></table1><table1><col1>Value2</col1></table1></XmlDS>";

System.IO.StringReader xmlSR = new System.IO.StringReader(xmlData);

dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);

Open in new window


Then you can use the DataSet visualizer to see the structure that is inferred from the XML.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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. 
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

16 Experts available now in Live!

Get 1:1 Help Now