vb.net - Parsing random XML strings -


in our sql server database have in text field records strings written xml random nodes i.e.

<record id="5">       <account>         <oldvalue>125025</oldvalue>         <newvalue></newvalue>     </account>       <amount>         <oldvalue>001995</oldvalue>         <newvalue></newvalue>     </amount>   </record id> 

or

<stock>     <car>         <oldvalue>035</oldvalue>         <newvalue>038 </newvalue>     </car> </stock> 

i need make these records readable in string report such as:

id: 5,  account old value: 125025,  account new value: - amount old value: 001995,  amount new value: - 

i don't think can serialize string because don't have predifined class serialize with. i'm in process of creating function read random xml nodes i'm not sure if i'm following right approach. more, @ moment i'm getting error: xmlexception: 'id' unexpected token.

public shared function xmlcustom(svalue string)     dim output stringbuilder = new stringbuilder()      if not string.isnullorempty(svalue)         dim xmlstring string = svalue         try             ' create xmlreader             using reader xmlreader = xmlreader.create(new stringreader(xmlstring))                 dim ws xmlwritersettings = new xmlwritersettings()                 ws.indent = true                 using writer xmlwriter = xmlwriter.create(output, ws)                      ' parse file , display each of nodes.                     while reader.read()                         select case reader.nodetype                             case xmlnodetype.element                                 writer.writestartelement(reader.name)                             case xmlnodetype.text                                 writer.writestring(reader.value)                             case xmlnodetype.xmldeclaration                             case xmlnodetype.processinginstruction                                 writer.writeprocessinginstruction(reader.name, reader.value)                             case xmlnodetype.comment                                  writer.writecomment(reader.value)                             case xmlnodetype.attribute                                  writer.writeelementstring("id", reader.value)                              case xmlnodetype.endelement                                  writer.writefullendelement()                         end select                     end while                 end using             end using          catch ex exception             msgbox(output.tostring())             messagebox.show("xmlcustom - " & ex.tostring)         end try     end if  end function 

any ideas how tackle problem?

without schema can not use serializer.
since of report tools support datatable type datasource, can collect data xml string datatable

'method read elements , generate columns datatable , save values 'method recursively read every element , add elements names column name public function readelement(el xelement, columnname stringbuilder) ilist(of tuple(of datacolumn, string))     dim temp new list(of tuple(of datacolumn, string))()     if el nothing return temp     columnname.append(el.name)     if el.haselements = true         each child xelement in el.elements             temp.addrange(readelement(child, columnname))         next     else         dim column new datacolumn(columnname.tostring(), gettype(string))         dim value string = el.value         dim item new tuple(of datacolumn, string)(column, value)         temp.add(item)     end if     return temp end function 

creating datatable

dim mainelement xelement = xelement.parse(yourxmlstringvalue)  dim data new datatable() data.columns.add("id", gettype(string))  'generate schema datatable dim scheemaandvalues new list(of tuple(of datacolumn, string))() each child xelement in mainelement.elements     dim columnname new stringbuilder()     scheemaandvalues.addrange(readelement(child, columnname)) next  'add columns datatable each arvo tuple(of datacolumn, string) in scheemaandvalues    data.columns.add(arvo.item1) next  'add values row dim dr datarow = data.newrow() dr.setfield(of string)("id", mainelement.attribute("id").value) each arvo tuple(of datacolumn, string) in scheemaandvalues    dr.setfield(arvo.item1.columnname, arvo.item2) next data.rows.add(dr) 

Comments