Solved

sorting XML

Posted on 2013-11-15
4
250 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

10 Experts available now in Live!

Get 1:1 Help Now