vb.net - Retrieve value in XML in vb -
i looking more efficient way retrive “f” @agesex in following xml using vb.
the following shortened version of our xml structure:
<gridrow xmlversion="1.0" columnnames="description|notes|detail question|detail question id"> <row> > ... </row> > ... <row> <nodelevel>0</nodelevel> <rowdata>cancer|@agesex=f;;|cancer - female|2320</rowdata> <rowstyle>|||</rowstyle> </row> </gridrow>
here have point.
xmldoc.loadxml(topproblemlisttemplate.templatexml) nodelist = xmldoc.selectnodes("/gridrow") each row xmlnode in nodelist each item xmlnode in row.childnodes if item.innertext.contains("@agesex") dim index integer = item.innertext.indexof("@agesex=") + len("@agesex=") if index > 0 dim value string = item.innertext.substring(index, 1) end if end if next next
using the: xmldocument class (msdn)
imports system.xml imports system.text.regularexpressions dim d new xmldocument d.load("file.xml") ' ... in specific case (or /gridrow/row/rowdata if want specific) ' ... select rowdata nodes in document @ least 1 level deep. dim _rownodes xmlnodelist = d.selectnodes(".//rowdata") each _rownode xmlnode in _rownodes if new regex("@agesex=.", regexoptions.none).match(s).tostring.endswith("f") ' ... dosomething end if next ' ... example dim _rownodes xmlnodelist = d.selectnodes("//gridrow") each _rownode xmlnode in _rownodes ' ... iterating on each gridrow each _childnode xmlnode in _rownode ' ... iterating on each row each _itemnode xmlnode in _childnode ' ... iterating on each item in row dim string = _itemnode.innertext.trim next ' ... or can node directly dim b string = _childnode.selectsinglenode("rowdata").innertext.trim ' ... specify properties (assuming fictional gender): dim g string = "female" dim c xmlnodelist = _childnode.selectnodes("rowdata[@gender='" & g & "']") ' ... per documentation, can match on value (the data contained in .innertext property) dim e string = "myvalue" dim f xmlnodelist = _childnode.selectnodes("rowdata[.='" & e & "']") next next
examples: how to: use system.xml.xmldocument class run xpath queries in visual basic .net
as document states, lang/notation used queries is: xpath examples (msdn) supports more selective queries, if know data you're looking for.
also, xml properties
defined such as:
<myitem name="myname" gender="female">value</myitem>
if gender property, easier, shown in example above, ie:
dim g = "f" : d.selectnodes(".//rowdata[@gender='" & g & "']")
;p
Comments
Post a Comment