c# - How to get all XML nodes with the same name without knowing their level? -
i have xml example:
<fruits> <red_fruits> <red_fruits></red_fruits> </red_fruits> <yellow_fruits> <banana></banana> </yellow_fruits> <red_fruits> <red_fruits></red_fruits> </red_fruits> </fruits>
i have 4 red_fruits tags, 2 of them shares same parentnode (fruits), want have same parentnode.
but want have same name (red_fruits), means yellow_fruits tag isn't included.
this way doing right using c# language:
xmldocument doc = new xmldocument(); string selectedtag = cmbx.text; if (file.exists(txtfile.text)) { try { //load doc.load(cmbfile.text); //select nodes xmlnodelist selectednodelist = doc.selectnodes(".//" + selectedtag); } catch { messagebox.show("some error message here"); } }
this returning me red_fruits, not ones belongs fruits.
i can't make xmlnodelist = doc.selectnodes("/fruits/red_fruits") because want use code read random xml files, don't know exact name specific node have, need put nodes same name , same level xmlnodelist using c# language.
is there way of achieve without using linq? how that?
if you're trying find "next" or "previous" iteration of single node, can following , compare name
xmlnode current = doc.selectsinglenode("fruits").selectsinglenode("red_fruits"); xmlnode previous = current.nextsibling; xmlnode next = current.nextsibling;
and can iterate until find proper sibling
while(next.name != current.name) { next = next.nextsibling; }
or can list invoking 'parent' property
xmlnodelist list = current.parentnode.selectnodes(current.name);
Comments
Post a Comment