c# - Create path from xml parent-child using attribute values -
i have following xml:
<?xml version="1.0" encoding="utf-8"?> <parent> <element href="www.something.com" title="first"> <element href="www.something.com" title="second"> <element href="www.something.com" title="third"> </element> </element> </element> <element href="www.something.com" title="some title"></element> <element href="www.something.com" title="another"> <element href="www.something.com" title="extra"> <element href="www.something.com" title="page"> <element href="www.something.com" title="target"> </element> </element> </element> </element> </parent>
how can parse xml , format path each element using title attribute parent down deepest children?
each element can have zero, 1 or more children.
example:
<element href="www.something.com" title="first"> // path: first <element href="www.something.com" title="second"> // path: first / second <element href="www.something.com" title="third"> //path: first / second /third </element> </element> </element> <element href="www.something.com" title="some title"></element> // path: title <element href="www.something.com" title="another"> // path: <element href="www.something.com" title="extra"> // path: / <element href="www.something.com" title="page"> // path: / / page <element href="www.something.com" title="target"> //path: / / page / target </element> </element> </element> </element>
parse using linq xml , build paths need. given element:
var titles = element.ancestorsandself() .selectmany(e => e.attributes("title")) .select(a => a.value) .reverse(); var path = string.join(" / ", titles);
Comments
Post a Comment