1

I have the following XML:

<TRUCK Counter="0">
  <CARRIER>A PLUS EXPEDITING &amp; LOGISTICS INC.</CARRIER>
  <CARRIERPHONE>(000)000-0220</CARRIERPHONE>
  <UNITNO>100673V</UNITNO>
  <UNITTYPE>CARGO VAN                     </UNITTYPE>
  <DISTANCE>13</DISTANCE>
  <AVAILABLE>06/14/2012 09:00</AVAILABLE>
  <STATUS>In Service</STATUS>
  <LOCATION>REDFORD, MI</LOCATION>
  <NOTE> check 1st</NOTE>
  <PAYLOAD>3000</PAYLOAD>
  <DIMS>
    <BOXLENGTH>108</BOXLENGTH>
    <BOXWIDTH>53</BOXWIDTH>
    <BOXHEIGHT>48</BOXHEIGHT>
  </DIMS>
  <DOMICILE>US</DOMICILE>
  <SATELLITE>N</SATELLITE>
</TRUCK>

I'm selecting the elements I want and sorting them in a method that gets passed the column to sort on:

public static XDocument GetSortedTrucksXml(string xmlBuffer, string sortColumn)
{
    XDocument xdoc = XDocument.Parse(xmlBuffer);

    int Counter = 0;

    var trucks = from s in xdoc.Element( "TRUCKS" ).Elements( "TRUCK" )
                 orderby ( string )s.Element( sortColumn )
                 select new XElement( "TRUCK",
                        s.Element( "CARRIER" ),
                        s.Element( "CARRIERPHONE" ),
                        s.Element( "UNITNO" ),
                        s.Element( "UNITTYPE" ),
                        s.Element( "DISTANCE" ),
                        s.Element( "AVAILABLE" ),
                        s.Element( "STATUS" ),
                        s.Element( "LOCATION" ),
                        s.Element( "NOTE" ),
                        s.Element( "PAYLOAD" ),
                        s.Element( "DIMS" ),
                        s.Element( "DOMICILE" ),
                        s.Element( "SATELLITE" ) );

    XDocument newDoc = new XDocument( new XElement( "TRUCKS" ) );

Note that I'm getting the children of by just specifying the parent element.

My question is how would I sort on BOXLENGTH, BOXWIDTH or BOXHEIGHT? If I send in "DIMS/BOXLENGTH" as the sort column, I get an error.

1
  • {"The '/' character, hexadecimal value 0x2F, cannot be included in a name."} Commented Jun 14, 2012 at 14:13

1 Answer 1

1

You can use the XPath Extension methods:

// You need this namespace
using System.Xml.XPath;

// use this in your query
orderby (string)s.XPathSelectElement(sortColumn)
Sign up to request clarification or add additional context in comments.

4 Comments

Are you suggesting to somehow change the orderby statement when the sortcolumn is BOXLENGTH, BOXWIDTH, or BOXHEIGHT? Is that possible?
@Brian I think i have found a better solution. Just use the XPath extensions, then you can even use the sortColumn parameter without changing it. Edited my post to include an example.
It is complaining about not having a definition for XPathSelectElement - I'm new to LINQ so, I'll have to figure out what I'm missing
@Brian You have to include the using statement at the top of your file: using System.Xml.XPath;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.