Solved

sorting XML

Posted on 2013-11-15
4
261 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:Mydeen Yussouf
  • 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:Mydeen Yussouf
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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