c# - Convert string with XML to datatable, list or something more manageable? -
without having manually parse file, .net
have convert string xml
xmlelement
, datatable
, list
or that's more manageable?
this actual string. string stored in xmlnode
object (this xmlnode.outerxml
), not know how convert can use:
<ni> <nss>20150927</nss> <gp>addon</gp> <ns>counterblah1</ns> <ns>counterblah2</ns> <ns>countersales</ns> <ns>counterblah4</ns> <ns>counterblah5</ns> <ns>counterblah6</ns> <nv> <nad>style=1,rfu=1,id=132</nad> <r>0</r> <r>15</r> <r>8</r> <r>3</r> <r>2</r> <r>2</r> </nv> <nv> <nad>style=1,rfu=1,id=433</nad> <r>0</r> <r>15</r> <r>30</r> <r>3</r> <r>2</r> <r>2</r> </nv> <nv> <nad>style=1,rfu=1,id=665</nad> <r>0</r> <r>15</r> <r>90</r> <r>3</r> <r>2</r> <r>2</r> </nv> </ni>
thanks.
using linq-to-xml
can data this
//using system.xml.linq; //using system.linq; //xmlstr = xml string var xdoc = xdocument.parse(xmlstr); var nvs = xdoc.descendants("nv"); var nads = nvs.select(nv => nv.elements("nad").first().value).tolist(); var thirdrs = nvs.select(nv => nv.elements("r").elementat(2).value).tolist();
this code generates, sample xml
nads = [ "style=1,rfu=1,id=132", "style=1,rfu=1,id=433", "style=1,rfu=1,id=665" ]
thirdrs = [ "8", "30", "90"]
Comments
Post a Comment