Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an XML stream that looks like the following (attribute and node quantity , and node depth reduced for demonstration purposes.)

<itinerary>
  <organizer>
    <company name="ACME" billingLocation="London" adminOffice="Manchester">
  </organizer>
  <traveller name="John Smith" homeCity="London" />
  <flight date="20160317" code="BA101" origin="London" destination="New York" />
  <vehicleHire start="20160317" end="20160317">
    <depot type="pickup" location="London" />
    <depot type="return" location="Heathrow" />
  </vehicleHire>
  <hotel location="New York" checkin="20160317" checkout="20160318" />
  <journey>
    <train date="20160318">
      <origin station="New York" />
      <destination station="Boston" />
    </train>
</itinerary>

I'd like to get a distinct list of attribute values where the attribute name is in (billingLocation,adminOffice,homeCity,origin,destination,location,station), but my final/actual list of attribute names will number around 30.

I have this C# code working using XmlDocument and an XPath query, but I'm unsure if this is the most efficient method.

XmlDocument xd = new XmlDocument();
xd.Load(customProvider.GetStream());
XmlNodeList nl = xd.SelectNodes(@"//@billingLocation|//@adminOffice|//@homeCity|//@origin|//@destination|//@location|//@station");
var lst = nl.Cast<XmlAttribute>().Select(a => a.Value).Distinct().ToList();

The final XML will be more complex and have a deeper hierarchy, and the attributes might occur across different nodes.

Is XmlDocument and XPath the most efficient aproach, or would Linq to XML perhaps be faster, and what would that look like?

share|improve this question
    
I don't understand, why would you want to combine cities and languages like this? Also, do you really not know anything about the elements that you want to search? – svick Mar 14 at 16:38
    
XML updated to be more realistic. Query now just seeks a distinct list of cities/locations – ThunderFrame Mar 16 at 22:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.