c# - Parse an xml document with "dynamic" nodes -


i parsing xml via xdocument, how can retreive languages, i.e <en> or <de> or <codecountry> , child elements?

<en>   <descriptif>in historic area, 16th century town house on 10,764 sq. ft. features 10 rooms , 3 shower-rooms. period features include spiral staircase. 2-room annex house vaulted cellar. period orangery. ref.: 2913.</descriptif>   <prox>nogent-le-rotrou.</prox>   <libelle>nogent-le-rotrou.</libelle> </en> <de>   <descriptif>`enter code here`in historic area, 16th century town house on 10,764 sq. ft. features 10 rooms , 3 shower-rooms. period features include spiral staircase. 2-room annex house vaulted cellar. period orangery. ref.: 2913.</descriptif>   <prox>nogent-le-rotrou.</prox> </de> ... <lang>   <descriptif></descriptif>   <prox></prox>   <libelle></libelle> </lang> 

as xml document not formatted, should first add root element. may that.

var content = file.readalltext(@"<path xml>"); var test = xdocument.parse("<language>" + content + "</language>"); 

then, have "dynamic top nodes", may try work children (which don't seem dynamic), assuming nodes have @ least "descriptif" child. (if it's not "descriptif", may "prox" or "libelle") **.

//this give parents, <en>, <de> etc. nodes var parents = test.descendants("descriptif").select(m => m.parent); 

then can select language , childrens. used anonymous type, can of course project custom class.

var allnodes = parents.select(m => new             {                 name = m.name.localname,                 descriptif = m.element("descriptif") == null ? string.empty : m.element("descriptif").value,                 prox = m.element("prox") == null ? string.empty : m.element("prox").value ,                 label = m.element("libelle") == null ? string.empty : m.element("libelle").value             }); 

this of course not performant code big file, but... that's problem.


** worst case, may do

var parents = test.descendants("descriptif").select(m => m.parent)                 .union(test.descendants("prox").select(m => m.parent))                 .union(test.descendants("libelle").select(m => m.parent)); 

Comments