Extract nodes from string containing xml
I am using a web service and get a SOAP envelope back and I am trying to extract some data but constantly run into errors. I have tried with LINQ, using xsl transform and so 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 my code behind I 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 is empty, I have also 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 then I get "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." and so on.
edit: So now I use (searchPayload is the string containing the 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 do I get the ABN from here in for example a 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: I realized now that I can do:
foreach(var item in ABN)
{
label1.Text += item.identifierValue.ToString();
}
@John Saunders is correct - you have to specify the namespace when retrieving data from your XML. Currently the namespace is set set in the XML:
<ABRSearchByABNResponse xmlns="some service /">
A quick verification using LINQ to XML retrieves the 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();
The ABRSearchByABNResponse
element and its descendants are in the "some service" namespace.
精彩评论