64985

Linq query - reading nested XML to dictionary

Question:

I am trying to parse nested XML file

The structure is (simplified) following:

<CARS_LIST_OUTPUT> <RESPONSE> <DATETIME>2014-09-03T12:12:55Z</DATETIME> <CARS_LIST> <CAR> <CID>12123</CID> <PARTS_LIST> <PART> <ID>QWEDS23</ID> </PART> <PART> <ID>QWEDS26</ID> </PART> </PARTS_LIST> </CAR> <CAR> <CID>44123</CID> <PARTS_LIST> <PART> <ID>QWED101</ID> </PART> <PART> <ID>QAADS23</ID> </PART> </PARTS_LIST> </CAR> </CARS_LIST> </RESPONSE> <CAR_LIST_OUTPUT>

I need to make a dictionary of all parts ID and their corresponding car id (CID), i.e.

QWEDS23, 12123 QWEDS26, 12123 QWED101, 44123 QAADS23, 44123

So far I got to this point:

var nodes = from c in carDirectory.Descendants("CAR") select new { parts = c.Descendants("PART") .Select(part => new { ID = part.Element("ID").Value, c.Element("CID").Value }).ToDictionary(r => r.ID) };

But this results in a list of dictionaries. Is it possible to achieve what I need? If yes, how can I improve my solution? Thank you for ideas!

Answer1:

Something like this should do the trick:

carDirectory.Descendants("CAR") .SelectMany(x => x.Descendants("ID") .Select(e => new KeyValuePair<string, string>( (string) e, (string) x.Element("CID")))) .ToDictionary(x => x.Key, x => x.Value);

Recommend

  • How to extract source from Google search result “20-pack” entry?
  • How to get parent id(root parent) from child id [duplicate]
  • Problem fetching XML data using Expedia API and curl
  • Passing callback parameters to setTimeout() does not work in IE?
  • Subclass HashSet so that it always uses a certain IEqualityComparer when used in another set
  • Auto-Renewable subscription for in-app purchase
  • Should this be giving a compile error?
  • Compare Dictionary
  • How can I seek key of dictionary by index value?
  • Django/Python: How to iterate through a list in a dictionary for migration/migrate data
  • rename keys in a dictionary
  • How to build PHP application with multiple language support (English, French, Chinese etc..)
  • How to token a word which combined by two words without whitespace
  • Confused about how to update my datasources on my UITableView after app has been released
  • C# Dictionary ContainsKey
  • Multiple for loop iterators to unpack in Python [duplicate]
  • Batching in py2neo
  • Automating table/object name scan and search in SAS
  • sql if statement checking for null parameter
  • retrofit.RetrofitError: 404 Not Found
  • Apache Lucene - Improving the results of Spell Checker
  • JDBI's @BindBean doesn't find named parameters in bean class during INSERT
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • How to use ResourceDictionary in Windows Phone class library project
  • Dynamically set properties from Dictionary in Swift
  • Add spaces between words in spaceless string
  • Creating JS objects in PHP with commas in between
  • Convert two columns Pandas data frame to dictionary of list with first column as keys
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • How do I bind multiple properties in an Android layout element
  • Xstream to map “choice” elements of XML
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • What is Eclipse's Declaration View used for?
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Append folder name and increment by 1 using batch script