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\"><cardtrxsummary>\r\n <paymentmethod>\r\n <payment_type_id>visa </payment_type_id>\r\n <authorization>0.0000</authorization>\r\n <capture>0.0000</capture>\r\n <forcecapture>0.0000</forcecapture>\r\n <postauth>0.0000</postauth>\r\n <return>0.0000</return>\r\n <sale>3419.2700</sale>\r\n <receipt>0.0000</receipt>\r\n <repeatsale>0.0000</repeatsale>\r\n <activate>0.0000</activate>\r\n <deactivate>0.0000</deactivate>\r\n <reload>0.0000</reload>\r\n <authorization_cnt>0</authorization_cnt>\r\n <capture_cnt>0</capture_cnt>\r\n <forcecapture_cnt>0</forcecapture_cnt>\r\n <postauth_cnt>0</postauth_cnt>\r\n <return_cnt>0</return_cnt>\r\n <sale_cnt>13</sale_cnt>\r\n <receipt_cnt>0</receipt_cnt>\r\n <repeatsale_cnt>0</repeatsale_cnt>\r\n <activate_cnt>0</activate_cnt>\r\n <deactivate_cnt>0</deactivate_cnt>\r\n <reload_cnt>0</reload_cnt>\r\n <cnt>13</cnt>\r\n </paymentmethod>\r\n</cardtrxsummary></string>"
which, assume, part of problem?
edit#2
this ended doing work. i'm sure there better way:
var tst2 = tst.innertext.replace("<", "<").replace(">", ">").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
Post a Comment