Solved

Get Element By xml string

  • 18 July 2023
  • 1 reply
  • 40 views

Userlevel 3
Badge

Hi,

I got a xml string as the json response body from the third party API.When I call the API.It;s response is json format.

{
"referenceId": "b7e0fdacd8bd",
"profileId": "PC12345678",
"statusCode": "ACCP",
"statusDesc": "Account transactions report generated successfully.",
"reportBase64": "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCEtLSBTYW1wbGUgWE1MIGZpbGUgZ2VuZXJhdGVkIGJ5IEZGRlMgKHd3dy5ma2wuZmkpIEIyQyBwcm9qZWN0IGZvciBGSW5uaXNoIEFjY291bnQgUmVwb3J0LCAyMDA5LTExLTAyLCBmb3IgZGlmZmVyZW50IEVudHJ5IGluc3RhbmNlIGZvciAtLT4KPCEtLSBNVDIxMCAvIFByZW5vdGljZSBvZiBpbmNvbWluZyBpbnRlciBuYXRpb25hbCB0cmFuc2FjdGlvbiAod2l0aG91dCBiYWxhbmNlcykgYW5kIG9uZSBzaW1wbGUgaW50cmEgZGF5IGJhbGFuY2UgcmVwb3J0IHdpdGggb25lIHRyYW5zYWN0aW9uLS0
}

reportBase64 has the encoded string.When I decode it ,It showed a xml string like below.

?xml version="1.0" encoding="UTF-8"?>
<!-- Sample XML file generated by FFFS (www.fkl.fi) B2C project for FInnish Account Report, 2009-11-02, for different Entry instance for -->
<!-- MT210 / Prenotice of incoming inter national transaction (without balances) and one simple intra day balance report with one transaction-->
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.052.001.02
camt.052.001.02.xsd">
<BkToCstmrAcctRpt>
<GrpHdr>
<MsgId>BANKFILEID219073</MsgId>
<CreDtTm>2009-10-30T12:01:45+02:00</CreDtTm>
<MsgRcpt>
<Id>
<OrgId>
<Othr>
<Id>123456789</Id>
<SchmeNm>
<Cd>BANK</Cd>
</SchmeNm>
</Othr>
</OrgId>
</Id>
</MsgRcpt>
<AddtlInf>Some additional message-specific info can be delivered from bank to customer here</AddtlInf>
</GrpHdr>
</BkToCstmrAcctRpt>
</Document>

I need to get the above MsgId xml element from above xml string.

I used the below query to extract element from xml.

var myDetails = JsonConvert.DeserializeObject<MyDetail>(responseJson.ToString());
byte[] mybyte = System.Convert.FromBase64String(myDetails.reportBase64);
string returntext = System.Text.Encoding.UTF8.GetString(mybyte);
order.OrderDesc = myDetails.statusDesc;
PXTrace.WriteInformation(returntext);
Base.Document.Update(order);

//xml read
string data = returntext;
XElement elem = XElement.Parse(data);
var test = elem.Element("BkToCstmrAcctRpt").Element("GrpHdr").Element("MsgId").Value;
if(test!=null){
PXTrace.WriteInformation(test);
}

But,It showed a null reference error in the trace.

icon

Best answer by praveenpo 18 July 2023, 06:36

View original

1 reply

Userlevel 6
Badge +3

hi @jeewanishalika20,

I recommend utilizing this code as it presents a solution that addresses the specific problem you are facing. By implementing this code, you should be able to overcome the challenges you encountered and achieve the desired outcome I encourage you to give it a try and see the positive results it can deliver.
 

           // Load the XML string into an XmlDocument
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            // Set up the namespace manager with the appropriate namespace
            XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
            nsManager.AddNamespace("ns", "urn:iso:std:iso:20022:tech:xsd:camt.052.001.02");

            // Get the value of the <MsgId> element using XPath
            string msgId = xmlDoc.SelectSingleNode("//ns:MsgId", nsManager)?.InnerText;

          

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved