c# - Read XML node from certain element forward? -
i know there bunch of ways of doing this, i'm looking easiest way of doing without having use streamreader or more manual force me loop through whole file , compare endless strings. possibility use streamreader find specific text, , use xml library retrieve node.
here's xml. need do:
1) find first instance of tag npid text text find
2) extract node nodetoextract , store in data object. there several nodes called nodetoextract want first 1 after initial search of text text find
<?xml version="1.0" encoding="iso-8859-1"?> <mdc> <ne> <neun>adb</neun> <nn>subnetwork=context</nn> <nw>r33</nw> <mi> <nut>20140101</nut> <hq>000</hq> <nw> <npid>text find</npid> <r>0</r> </nw> </mi> </ne> <ofid> <ofun>abc</ofun> <ofdn>blah</ofdn> <ofsw>18r</ofsw> </ofid> <nodetoextract> <mts>more blah</mts> <gp>000</gp> <mu>value1</mu> <mu>value2</mu> <mu>value3</mu> <mu>value4</mu> <mu>value5</mu> <nw> <npid>abc1221</npid> <r>99</r> <r>0</r> <r>0</r> <r>0</r> <r>0</r> </nw> <nw> <npid>abc1222</npid> <r>99</r> <r>0</r> <r>0</r> <r>0</r> <r>0</r> </nw> <nw> <npid>abc1223</npid> <r>99</r> <r>0</r> <r>0</r> <r>0</r> <r>0</r> </nw> </nodetoextract> </mdc>
any appreciated.
try out (assuming mean mi
elements saying nodetoextract
):
xelement doc = xelement.parse(@"<?xml version=""1.0"" encoding=""iso-8859-1""?><mdc><ne><neun>adb</neun><nn>subnetwork=context</nn><nw>r33</nw><mi><nut>20140101</nut><hq>000</hq><nw><npid>text find</npid><r>0</r></nw></mi></ne><ofid><ofun>abc</ofun><ofdn>blah</ofdn><ofsw>18r</ofsw></ofid><mi><mts>more blah</mts><gp>000</gp><mu>value1</mu><mu>value2</mu><mu>value3</mu><mu>value4</mu><mu>value5</mu><nw><npid>abc1221</npid><r>99</r><r>0</r><r>0</r><r>0</r><r>0</r></nw><nw><npid>abc1222</npid><r>99</r><r>0</r><r>0</r><r>0</r><r>0</r></nw><nw><npid>abc1223</npid><r>99</r><r>0</r><r>0</r><r>0</r><r>0</r></nw></mi></mdc>"); ienumerable<xelement> query1 = doc.descendants("npid").where(c => c.value == "text find").ancestors("mi"); ienumerable<xelement> query2 = query1.first().parent.elementsafterself("mi"); dynamic resut_node = new xelement("result"); resut_node.add(query1.first()); resut_node.add(query2.first()); console.write(resut_node.tostring());
update
xelement doc = xelement.parse(@"<?xml version=""1.0"" encoding=""iso-8859-1""?> <mdc> <ne> <neun>adb</neun> <nn>subnetwork=context</nn> <nw>r33</nw> <mi> <nut>20140101</nut> <hq>000</hq> <nw> <npid>text find</npid> <r>0</r> </nw> </mi> </ne> <ofid> <ofun>abc</ofun> <ofdn>blah</ofdn> <ofsw>18r</ofsw> </ofid> <nodetoextract> <mts>more blah</mts> <gp>000</gp> <mu>value1</mu> <mu>value2</mu> <mu>value3</mu> <mu>value4</mu> <mu>value5</mu> <nw> <npid>abc1221</npid> <r>99</r> <r>0</r> <r>0</r> <r>0</r> <r>0</r> </nw> <nw> <npid>abc1222</npid> <r>99</r> <r>0</r> <r>0</r> <r>0</r> <r>0</r> </nw> <nw> <npid>abc1223</npid> <r>99</r> <r>0</r> <r>0</r> <r>0</r> <r>0</r> </nw> </nodetoextract> </mdc>"); ienumerable<xelement> query1 = doc.descendants("npid").where(c => c.value == "text find").ancestors("mi"); ienumerable<xelement> query2 = query1.first().parent.elementsafterself("nodetoextract"); console.write(query2.first().tostring());
Comments
Post a Comment