Dealing with awkward XML layout in c# using XmlTextReader -


so have xml document i'm trying import using xmltextreader in c#, , code works except 1 part, that's tag line not on same line text/content, example product_name:

    <product>          <sku>27939</sku>          <product_name>             sof-therm warm-up jacket         </product_name>          <supplier_number>alnn1064</supplier_number>      </product> 

my code try sort xml document such:

while (reader.read())             {                 switch (reader.name)                 {                     case "sku":                         newele = new xmlelement();                         newele.sku = reader.readstring();                         break;                     case "product_name":                         newele.productname = reader.readstring();                         break;                     case "supplier_number":                         newele.suppliernumber = reader.readstring();                         products.add(newele);                         break;                 }             } 

i have tried found in xmltextreader documentation

reader.movetoelement(); reader.movetocontent(); reader.movetonextattribute(); 

and couple others made less sense, none of them seem able consistently deal issue. fix 1 case, break regular cases. question is, there way have after find "product_name" tag go next line contains text , extract it?

i should have mentioned, outputting html table after , element coming blank i'm not reading correctly.

thanks in advanced!

i think find linq xml easier use

var xdoc = xdocument.parse(xmlstring); //or xdocument.load(filename);  int sku = (int)xdoc.root.element("sku"); string name = (string)xdoc.root.element("product_name"); string supplier = (string)xdoc.root.element("supplier_number"); 

you can convert xml dictionary

var dict = xdoc.root.elements()            .todictionary(e => e.name.localname, e => (string)e);  console.writeline(dict["sku"]); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -