c# xml xpath create attribute if not in document -
how can create attribute if doesn't exist?
/document/setup/info/att/group[@name='manu']/osis:attr[@name='time']/@value
some of documents have value attribute don't.
xml (with time no value attribute):
<?xml version="1.0" encoding="utf-8"?> <document xmlns="http://www.ns.com/ns/ns"> <setup> <info> <filepath>\\computer1\project\e2002307\e2002307.drg</filepath> <att> <group class="custom" name="manu" desc="attributes" ord="6"> <attr num="119" name="xyz" desc="zyx" type="s" ord="1" value="s355"> <valid perm="e" max="100"/> </attr> <attr num="120" name="thick" desc="thick." type="r" ord="2" value="5"> <valid perm="e" min="0" max="99999"/> </attr> <attr num="121" name="units" desc="units." type="s" ord="4" value="mm"> <valid perm="e" expr="mm" max="80"/> </attr> <attr num="123" name="time" desc="minutes." type="r" ord="24"> <valid perm="e" min="0"/> </attr> <attr num="124" name="x" desc="x direction." type="r" ord="11" value="3"> <valid perm="e" min="0"/> </attr> <attr num="125" name="y" desc="y direction." type="r" ord="12" value="1"> <valid perm="e" min="0"/> </attr> </group>
modify xpath bit make return <attr>
elements don't have value
attribute instead of returning attribute :
/document/setup/info/att/group[@name='manu']/osis:attr[@name='time' , not(@value)]
then loop through xpath result , add attribute needed :
xmldocument doc; ..... var query = "/document/setup/info/att/group[@name='manu']/osis:attr[@name='time' , not(@value)]"; var nodes = doc.selectnodes(query, xmlns); foreach(xmlnode node in nodes) { xmlattribute attr = doc.createattribute("value"); attr.value = "foo"; node.attributes.append(attr); }
Comments
Post a Comment