I am building a service who receive xml payload and after treating my internal request respond a new xml response to the caller
Now to facilitate the work i created a XML template where i have put all my template in xml files.
below is the content of my xml template.
<?xml version="1.0" encoding="utf-8" ?>
<FPEnvelope xmlns="namespace1"
xmlns:document="namespace2"
xmlns:header="namespace3">
<header:AppHdr>
<header:Fr>
<header:FIId>
<header:FinInstnId>
<header:Othr>
<header:Id>{{SWITCH}}</header:Id>
</header:Othr>
</header:FinInstnId>
</header:FIId>
</header:Fr>
<header:To>
<header:FIId>
<header:FinInstnId>
<header:Othr>
<header:Id>{{BIC2}}</header:Id>
</header:Othr>
</header:FinInstnId>
</header:FIId>
</header:To>
<header:BizMsgIdr>{{BizMsgIdr}}</header:BizMsgIdr>
<header:MsgDefIdr>pacs.002.001.12</header:MsgDefIdr>
<header:CreDt>{{CreDt}}</header:CreDt>
<header:Rltd>
<header:Fr>
<header:FIId>
<header:FinInstnId>
<header:Othr>
<header:Id>{{BIC2}}</header:Id>
</header:Othr>
</header:FinInstnId>
</header:FIId>
</header:Fr>
<header:To>
<header:FIId>
<header:FinInstnId>
<header:Othr>
<header:Id>{{SWITCH}}</header:Id>
</header:Othr>
</header:FinInstnId>
</header:FIId>
</header:To>
<header:BizMsgIdr>{{BizMsgIdr}}</header:BizMsgIdr>
<header:MsgDefIdr>pacs.002.001.12</header:MsgDefIdr>
<header:CreDt>{{CreDt}}</header:CreDt>
</header:Rltd>
</header:AppHdr>
<document:Document>
<document:FIToFIPmtStsRpt>
<document:GrpHdr>
<document:MsgId>{{MsgId}}</document:MsgId>
<document:CreDtTm>{{CreDtTm}}</document:CreDtTm>
<document:InstgAgt>
<document:FinInstnId>
<document:Othr>
<document:Id>{{BIC2}}</document:Id>
</document:Othr>
</document:FinInstnId>
</document:InstgAgt>
<document:InstdAgt>
<document:FinInstnId>
<document:Othr>
<document:Id>{{BIC1}}</document:Id>
</document:Othr>
</document:FinInstnId>
</document:InstdAgt>
</document:GrpHdr>
<document:TxInfAndSts>
<document:OrgnlEndToEndId>{{OrgnlEndToEndId}}</document:OrgnlEndToEndId>
<document:OrgnlGrpInf>
<document:OrgnlMsgId>{{OrgnlMsgId}}</document:OrgnlMsgId>
<document:OrgnlMsgNmId>pacs.008.001.10</document:OrgnlMsgNmId>
</document:OrgnlGrpInf>
<document:OrgnlTxId>{{OrgnlTxId}}</document:OrgnlTxId>
<document:TxSts>ACSC</document:TxSts>
<document:AccptncDtTm>
{{AccptncDtTm}}
</document:AccptncDtTm>
<document:OrgnlTxRef>
<document:IntrBkSttlmAmt Ccy="GNF">{{BkSttlmAmt}}</document:IntrBkSttlmAmt>
<document:Amt>
<document:InstdAmt Ccy="GNF">{{Amount}}</document:InstdAmt>
</document:Amt>
<document:Dbtr>
<document:Nm>{{Nom}}</document:Nm>
<document:PstlAdr>
<document:AdrLine>{{AdrLine}}</document:AdrLine>
</document:PstlAdr>
</document:Dbtr>
<document:DbtrAcct>
<document:Id>
<document:Othr>
<document:Id>{{DbtrAcct_id}}</document:Id>
<document:SchmeNm>
<document:Prtry>ACCT</document:Prtry>
</document:SchmeNm>
</document:Othr>
</document:Id>
</document:DbtrAcct>
<document:CdtrAcct>
<document:Id>
<document:Othr>
<document:Id>CdtrAcct_id</document:Id>
<document:SchmeNm>
<document:Prtry>ACCT</document:Prtry>
</document:SchmeNm>
</document:Othr>
</document:Id>
</document:CdtrAcct>
</document:OrgnlTxRef>
</document:TxInfAndSts>
</document:FIToFIPmtStsRpt>
</document:Document>
</FPEnvelope>
what i really want is retrieve elements with {{}} and change thier content like {{SWITCH}} and replace it by his right content with my code, bellow is my code pls: i have created a method to read a xml file:
public XDocument readxml_file()
{
var path = Path.Combine(_hostingEnvironment.WebRootPath, "XmlTemplates/NotificationResponse.xml");
XDocument doc = XDocument.Load(path);
//Console.WriteLine(doc);
return doc;
}
and i am reading the file like this:
XDocument response_document = readxml_file();
now i want to retrieve elements with those brackets {{}} but i cant. i have tried:
var xElement = response_document.XPathSelectElement("//FPEnvelope/header");
var x = response_document.Descendants("header")
.Where(ele => (string)ele.Attribute("id") == "{{SWITCH}}");
after filling all those brackets content i need to send a response xml.
please can someone help me, thanks for your time.
Try following :
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
XNamespace nsDocument = root.GetNamespaceOfPrefix("document");
XNamespace nsHeader = root.GetNamespaceOfPrefix("header");
XElement appHdr = doc.Descendants(nsHeader + "AppHdr").FirstOrDefault();
XElement id = appHdr.Descendants(nsHeader + "Id").FirstOrDefault();
id.SetValue("New Value");
}