c# - parsing an xmldocument from a webrequest -


i'm having heck of time parsing layout:

<string xmlns="http://www.namespaceuri.com/admin/ws">     <cardtrxsummary>          <paymentmethod>              <payment_type_id>visa </payment_type_id>              <authorization>0.0000</authorization>              <capture>0.0000</capture> <forcecapture>0.0000</forcecapture>              <postauth>0.0000</postauth> <return>0.0000</return>              <sale>3419.2700</sale> <receipt>0.0000</receipt>              <repeatsale>0.0000</repeatsale>              <activate>0.0000</activate>              <deactivate>0.0000</deactivate>              <reload>0.0000</reload>              <authorization_cnt>0</authorization_cnt>              <capture_cnt>0</capture_cnt>              <forcecapture_cnt>0</forcecapture_cnt>              <postauth_cnt>0</postauth_cnt>              <return_cnt>0</return_cnt>              <sale_cnt>13</sale_cnt>              <receipt_cnt>0</receipt_cnt>              <repeatsale_cnt>0</repeatsale_cnt>              <activate_cnt>0</activate_cnt>              <deactivate_cnt>0</deactivate_cnt>              <reload_cnt>0</reload_cnt>              <cnt>13</cnt>          </paymentmethod>      </cardtrxsummary> </string> 

i trying code specific result:

private static string readvaluefromxml(xmldocument xmldocument, string field) {   var xdoc = xmldocument.toxdocument();   var ns = "http://www.namespaceuri.com/admin/ws";   return xdoc.descendants(ns + "paymentmethod")              .select(x => (string) x.attribute("cnt"))              .firstordefault(); } 

at point, it's giving me message:

the ':' character, hexadecimal value 0x3a, cannot included in name.

i tried way:

  xmlnodelist xnlist = xmldocument.selectnodes("/cardtrxsummary/paymentmethod");   foreach (xmlnode xn in xnlist)   {     console.writeline("sale: " + xn["sale"].innertext);     console.writeline("sale_cnt: " + xn["sale_cnt"].innertext);     console.writeline("payment_type_id: " + xn["payment_type_id"].innertext);   } 

and never went inside foreach.

how values within paymentmethod?

edit

i looked @ xmldocument's innertext , how it's displayed:

"<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://www.namespaceuri.com/admin/ws\">&lt;cardtrxsummary&gt;\r\n  &lt;paymentmethod&gt;\r\n    &lt;payment_type_id&gt;visa      &lt;/payment_type_id&gt;\r\n    &lt;authorization&gt;0.0000&lt;/authorization&gt;\r\n    &lt;capture&gt;0.0000&lt;/capture&gt;\r\n    &lt;forcecapture&gt;0.0000&lt;/forcecapture&gt;\r\n    &lt;postauth&gt;0.0000&lt;/postauth&gt;\r\n    &lt;return&gt;0.0000&lt;/return&gt;\r\n    &lt;sale&gt;3419.2700&lt;/sale&gt;\r\n    &lt;receipt&gt;0.0000&lt;/receipt&gt;\r\n    &lt;repeatsale&gt;0.0000&lt;/repeatsale&gt;\r\n    &lt;activate&gt;0.0000&lt;/activate&gt;\r\n    &lt;deactivate&gt;0.0000&lt;/deactivate&gt;\r\n    &lt;reload&gt;0.0000&lt;/reload&gt;\r\n    &lt;authorization_cnt&gt;0&lt;/authorization_cnt&gt;\r\n    &lt;capture_cnt&gt;0&lt;/capture_cnt&gt;\r\n    &lt;forcecapture_cnt&gt;0&lt;/forcecapture_cnt&gt;\r\n    &lt;postauth_cnt&gt;0&lt;/postauth_cnt&gt;\r\n    &lt;return_cnt&gt;0&lt;/return_cnt&gt;\r\n    &lt;sale_cnt&gt;13&lt;/sale_cnt&gt;\r\n    &lt;receipt_cnt&gt;0&lt;/receipt_cnt&gt;\r\n    &lt;repeatsale_cnt&gt;0&lt;/repeatsale_cnt&gt;\r\n    &lt;activate_cnt&gt;0&lt;/activate_cnt&gt;\r\n    &lt;deactivate_cnt&gt;0&lt;/deactivate_cnt&gt;\r\n    &lt;reload_cnt&gt;0&lt;/reload_cnt&gt;\r\n    &lt;cnt&gt;13&lt;/cnt&gt;\r\n  &lt;/paymentmethod&gt;\r\n&lt;/cardtrxsummary&gt;</string>" 

which, assume, part of problem?

edit#2

this ended doing work. i'm sure there better way:

  var tst2 = tst.innertext.replace("&lt;", "<").replace("&gt;", ">").replace("\r\n", string.empty);   console.writeline("cnt: " + readxmlvalue1(tst2, "cnt")); 

and method parse it:

private static void readxmlvalue1(string xmldocument) {   xdocument xdoc = xdocument.parse(xmldocument);   //xnamespace ns = "http://www.namespaceuri.com/admin/ws";   var payments = p in xdoc.descendants("paymentmethod")                  select new                  {                    sale = (decimal)p.element("sale"),                    salecount = (int)p.element("sale_cnt"),                    paymenttype = (string)p.element("payment_type_id")                  };   console.writeline("count: " + payments.count());   foreach (var payment in payments)   {     console.writeline("sale: " + payment.sale);     console.writeline("sale_cnt: " + payment.salecount);     console.writeline("payment_type_id: " + payment.paymenttype);   }  } 

edit#3

this how i'm creating xmldocument:

/// <summary> ///  data in xml format url /// </summary> /// <param name="url"></param> /// <returns></returns> private static xmldocument getxmldatafromurl(string url) {   //requesting particular web page   var httprequest = (httpwebrequest)webrequest.create(url);    //geting response request url   var response = (httpwebresponse)httprequest.getresponse();    //create stream hold contents of response (in case contents of xml file   var receivestream = response.getresponsestream();    //creating xml document   var mysourcedoc = new xmldocument();    //load file stream   if (receivestream != null)   {     mysourcedoc.load(receivestream);      //close stream     receivestream.close();     return mysourcedoc;   }   return null; } 

you can use linq xml list of typed anonymous payment objects:

webclient client = new webclient(); string content = client.downloadstring(url);  xdocument xdoc = xdocument.parse(content); xnamespace ns = "http://www.namespaceuri.com/admin/ws"; var payments = p in xdoc.descendants(ns + "paymentmethod")                 select new {                     sale = (decimal)p.element(ns + "sale"),                     salecount = (int)p.element(ns + "sale_cnt"),                     paymenttype = (string)p.element(ns + "payment_type_id")                 }; 

keep in mind, xml has namespace declared, should provide when specifying element names.

usage:

foreach(var payment in payments) {    console.writeline("sale: " + payment.sale);    console.writeline("sale_cnt: " + payment.salecount);    console.writeline("payment_type_id: " + payment.paymenttype); } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -