c# - Extract nodes from string containing xml -


i using web service , soap envelope , trying extract data run errors. have tried linq, using xsl transform , on.

the response is:

<?xml version="1.0" encoding="utf-8" ?>  - <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> - <soap:body> - <abrsearchbyabnresponse xmlns="some service /"> - <abrpayloadsearchresults> - <request> - <identifiersearchrequest>   <authenticationguid>some guid</authenticationguid>    <identifiertype>abn</identifiertype>    <identifiervalue>54 108 408 566</identifiervalue>    <history>n</history>    </identifiersearchrequest>   </request> - <response>   <usagestatement>some statementusagestatement>    <dateregisterlastupdated>2011-01-26</dateregisterlastupdated>    <datetimeretrieved>2011-01-26t15:14:45.9776800+11:00</datetimeretrieved>  - <businessentity>   <recordlastupdateddate>2000-08-26</recordlastupdateddate>  - <abn>   <identifiervalue>11111111111</identifiervalue>    <iscurrentindicator>y</iscurrentindicator>    <replacedidentifiervalue xsi:nil="true" />    <replacedfrom>0001-01-01</replacedfrom>    </abn> - <entitystatus>   <entitystatuscode>active</entitystatuscode>    <effectivefrom>2000-06-05</effectivefrom>    <effectiveto>0001-01-01</effectiveto>    </entitystatus>   <asicnumber />  - <entitytype>   <entitytypecode>ind</entitytypecode>    <entitydescription>individual/sole trader</entitydescription>    </entitytype> - <goodsandservicestax>   <effectivefrom>2000-07-01</effectivefrom>    <effectiveto>0001-01-01</effectiveto>    </goodsandservicestax> - <legalname>   <givenname>some name</givenname>    <othergivenname />    <familyname>some name</familyname>    <effectivefrom>2000-08-26</effectivefrom>    <effectiveto>0001-01-01</effectiveto>    </legalname> - <mainbusinessphysicaladdress>   <statecode>qld</statecode>    <postcode>4350</postcode>    <effectivefrom>2000-08-26</effectivefrom>    <effectiveto>0001-01-01</effectiveto>    </mainbusinessphysicaladdress>   </businessentity>   </response>   </abrpayloadsearchresults>   </abrsearchbyabnresponse>   </soap:body>   </soap:envelope> 

my xslt file:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:soap="http://soap/envelope/body/">     <xsl:output method="html" indent="yes"/>      <xsl:template match="/">       <table>         <tr>           <th>name</th>         </tr>         <xsl:for-each select="soap/abrsearchbyabnresponse/abrpayloadsearchresults/response/businessentity/legalname">           <tr>             <td>               <xsl:value-of select="givenname"/>             </td>                 </tr>          </xsl:for-each>        </table>     </xsl:template> </xsl:stylesheet> 

in code behind have tried with:

xmltextreader reader = new xmltextreader(new stringreader(searchpayload)); string xslpath = @"http://localhost:5434/searchresult.xslt"; xslcompiledtransform transform = new xslcompiledtransform(); transform.load(xslpath); transform.transform(reader, null, response.output); 

but name empty, have tried with

xmldocument xmldoc = new xmldocument(); xmldoc.load(new stringreader(searchpayload));  xmlnodelist xnlist = xmldoc.selectnodes("/soap:envelope/soap:body/abrsearchbyabnresponse/abrpayloadsearchresults/request/response/businessentity/abn"); foreach(xmlnode xn in xnlist) {     string abn = xn["identifiervalue"].innertext;     label1.text = abn; } 

but "namespace manager or xsltcontext needed. query has prefix, variable, or user-defined function." , on.

edit: use (searchpayload string containing xml response)

xdocument doc = xdocument.load(new stringreader(searchpayload)); xnamespace ns = @"http://abr.business.gov.au/abrxmlsearch/";  var abn = doc.descendants(ns + "businessentity")              .elements(ns + "abn")              .select(node => new              {                  identifiervalue = node.element(ns + "identifiervalue").value,                  iscurrentindicator = node.element(ns + "iscurrentindicator").value,                  replacedidentifiervalue = node.element(ns + "replacedidentifiervalue").value,                  replacedfrom = node.element(ns + "replacedfrom").value              }).tolist(); 

how abn here in example textbox?

`label1.text = abn.tostring();` gives me results:system.collections.generic.list`1[<>f__anonymoustype0`4[system.string,system.string,system.string,system.string]] 

edit2: realized can do:

foreach(var item in abn) {     label1.text += item.identifiervalue.tostring(); } 

@john saunders correct - have specify namespace when retrieving data xml. namespace set set in xml:

 <abrsearchbyabnresponse xmlns="some service /"> 

a quick verification using linq xml retrieves results successfully.

xdocument doc = xdocument.load(@"test.xml"); xnamespace ns = "some service /";  var abn = doc.descendants(ns + "businessentity")              .elements(ns + "abn")              .select(node => new              {                 identifiervalue = node.element(ns + "identifiervalue").value,                 iscurrentindicator = node.element(ns + "iscurrentindicator").value,                 replacedidentifiervalue = node.element(ns + "replacedidentifiervalue").value,                 replacedfrom = node.element(ns + "replacedfrom").value              }).tolist();  

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -