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?