Friday, May 17, 2013

Create an XML Document on the fly

I want to produce this:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CreateLoanPrincipalPayment SYSTEM "CreateLoanPrincipalPayment.dtd">
<CreateLoanPrincipalPayment
loanAlias="CHG8F47RP"
effectiveDate="10/07/2005"
requestedAmount="1000000"
version="1.0">
</CreateLoanPrincipalPayment>

C# Code:


using Services.LoanIq;


        private string GetXml(string functionName)
        {
            try
            {
                XDocument xdoc = new XDocument();
                XDocumentType documentType = new XDocumentType("CreateLoanPrincipalPayment",
                                                 null,
                                                 "CreateLoanPrincipalPayment.dtd",
                                                 null);
                xdoc.AddFirst(documentType);
                xdoc.Declaration = new XDeclaration("1.0",
                                       "utf-8",
                                       null); // Note: this will get dropped off automatically
                xdoc.Add(new XElement("CreateLoanPrincipalPayment",
                             new XAttribute("version", "1.0"),
                             new XAttribute("requestedAmount", "1000000"),
                             new XAttribute("effectiveDate", "10/07/2005"),
                             new XAttribute("loanAlias", "CHG8F47RP")));

                //So you have to add it back here
                string returnValue=xdoc.Declaration + Environment.NewLine + xdoc;
               
                return returnValue;

            }
            catch (Exception ex)
            {
               
                throw;
            }
        }



No comments:

Post a Comment