Parsing complicated xml with VB.Net: elements into strings depending on namespace -


i have following xml in file (simplified):

<?xml version="1.0" encoding="iso-8859-1"?> <xcer xmlns="http://x.y.z" xmlns:xsi="http://www.x.y.z" xsi:schemalocation="http://www.x.y.z" track_id="559" mp_id="398" sub_id="569"> <capability xsi:type="xcract"> <type>rec</type> <sub_type>pc</sub_type> <action>reco</action> </capability>  <final_result od="dgs=1.6" creator="creator1" version="1.11" xsi:type="xcart"> <code>300000000</code> <code_cnf>0.7454</code_cnf> <code_attr>seq</code_attr> <status_attr>fdos</status_attr> <text>this text</text> <standardized_text>other text</standardized_text> <region>   <type>add</type>   <symbology>machine</symbology> </region> </final_result>  <final_result od="dgs=1.7" creator="creator2" version="1.11" xsi:type="xcart"> <code>3040280100015</code> <code_cnf>0.7454</code_cnf> <code_attr>seq</code_attr> <status_attr>fdos</status_attr> <text>this text</text> <standardized_text>other text</standardized_text> <region>   <type>add</type>   <symbology>machine</symbology> </region>     <polygon>     <dot x="849" y="1600"/>     <dot x="823" y="1600"/>     <dot x="819" y="1166"/>     <dot x="845" y="1166"/>     </polygon> </final_result> </xcer> 

on basic level want create 3 variables: creator, code, mp_id , fill them details final result od="dgs=1.6" section i.e. 'creator1', '300000000', '398' (from first xcer element) , 'this text' xml skills soreley lacking despite trying several crash courses in last couple of days.

i have tried basic

using reader xmlreader = xmlreader.create("c:\filename.xml")   while reader.read()   if reader.isstartelement()   if reader.name = "code"   code = reader.readelementcontentasstring()   

which gets me code cannot of elements within line
final_result od="dgs=1.6" creator="creator1" version="1.11" xsi:type="xcart">

and cannot limit code element want subtree without on writing previous code.

here's simple example of how using xpath:

dim doc new xmldocument() doc.load("test.xml") dim namespacemanager new xmlnamespacemanager(doc.nametable) namespacemanager.addnamespace("x", "http://x.y.z") dim mp_id string = doc.selectsinglenode("/x:xcer[1]/@mp_id", namespacemanager).innertext dim creator string = doc.selectsinglenode("/x:xcer[1]/x:final_result[@od='dgs=1.6']/@creator", namespacemanager).innertext dim code string = doc.selectsinglenode("/x:xcer[1]/x:final_result[@od='dgs=1.6']/x:code", namespacemanager).innertext 

notice needed specify namespace, since elements have default namespace of http://x.y.z. purposes, gave namespace prefix of x, name want.

xpath standard language used querying xml documents. people prefer microsoft's proprietary linq technology, since xpath used other languages, tools, , technologies, worth taking time learn it. selectsinglenode , selectnodes methods allow find matching nodes using xpath.

the first xpath, selects mp_id, looks this:

/x:xcer[1]/@mp_id

  • / - start @ root of document
  • x:xcer - find element named xcer (in x namespace)
  • [1] - select first xcer element
  • / - descendant node of first xcer element
  • @mp_id - select mp_id attribute (which descendant node of first xcer element)

the next xpath, selects creator attribute, looks this:

/x:xcer[1]/x:final_result[@od='dgs=1.6']/@creator

this 1 starts same last one, instead of selecting attribute of xcer element, selects final_result child-element. [@od='dgs=1.6'] conditional clause. can read "select final_result element od attribute equals dgs=1.6".


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -