从数据集创建自定义XML文件

时间:2016-10-23 08:35:45

标签: xml dataset xmlwriter

我有一个数据集,我想从中创建一个XML文件。我想要创建的XML文件有一些自定义层次结构/元素。请检查我想要创建XML文件的以下XML格式。

<?xml version="1.0"?>

-<soapenv:Envelope xmlns:init="http://www.kotak.com/PaymentTransactionService/InitiateRequest/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<soapenv:Header/>

-<soapenv:Body>

-<init:InitiateRequestRoot>

-<init:RequestHeader>

<init:MessageId>MSG-1</init:MessageId>

<init:MsgSource>BILLDESK</init:MsgSource>

<init:ClientCode>0KMMF</init:ClientCode>

<init:BatchRefNmbr>BATCHREF-1</init:BatchRefNmbr>

</init:RequestHeader>

-<init:InstrumentList>

<!--1 or more repetitions:-->

-<init:instrument>

<init:InstRefNo>10000000000000000001</init:InstRefNo>

<!--Optional:-->

<init:CompanyId>DUMMY</init:CompanyId>

<init:CompBatchId>BATCHID1</init:CompBatchId>

<init:ConfidentialInd>N</init:ConfidentialInd>

<init:MyProdCode>EPAYMENTS</init:MyProdCode>

<init:CompTransNo>DUMMY</init:CompTransNo>

<init:PayMode>IFT</init:PayMode>

<init:TxnAmnt>120</init:TxnAmnt>

<init:DrRefNmbr>DEBITREF</init:DrRefNmbr>

<init:DrDesc>DRDESC</init:DrDesc>
</init:instrument>

首先,我想创建Request Header,之后我想要“Instrument list”元素,在该元素下将显示所有数据集记录。我怎么能这样做......任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

使用xml linq。像这样的东西

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string headerStr =
                "<?xml version=\"1.0\"?>" +
                "<soapenv:Envelope xmlns:init=\"http://www.kotak.com/PaymentTransactionService/InitiateRequest/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "</soapenv:Envelope>";

            XDocument soap = XDocument.Parse(headerStr);
            XElement envelope = (XElement)soap.FirstNode;
            XNamespace soapNs = envelope.GetNamespaceOfPrefix("soapenv");
            XNamespace initNs = envelope.GetNamespaceOfPrefix("init");

            XElement header = new XElement(soapNs + "Header", new object[] {
                new XElement(soapNs + "Body", new object[] {
                    new XElement(initNs + "InitiateRequestRoot")
                })
            });
            XElement initiateRequestRoot = header.Descendants(initNs + "InitiateRequestRoot").FirstOrDefault();
            envelope.Add(header);

            XElement requestHeader = new XElement(initNs + "RequestHeader", new object[] {
                new XElement(initNs + "MessageId", "MSG-1"),
                new XElement(initNs + "MsgSource","BILLDESK"),
                new XElement(initNs + "ClientCode","0KMMF"),
                new XElement(initNs + "BatchRefNmbr","BATCHREF-1"),
            });
            initiateRequestRoot.Add(requestHeader);

            XElement instrumentList = new XElement(initNs + "InstrumentList", new object[] {
                new XElement(initNs + "instrument")
            });
            initiateRequestRoot.Add(instrumentList);
            XElement instrument = header.Descendants(initNs + "instrument").FirstOrDefault();

            instrument.Add(new object[] {
               new XElement(initNs + "InstRefNo", "10000000000000000001"),
               new XElement(initNs + "CompanyId", "DUMMY"),
               new XElement(initNs + "CompBatchId", "BATCHID1"),
               new XElement(initNs + "ConfidentialInd", "N"),
               new XElement(initNs + "MyProdCode", "EPAYMENTS"),
               new XElement(initNs + "CompTransNo", "DUMMY"),
               new XElement(initNs + "PayMode","IFT"),
               new XElement(initNs + "TxnAmnt", "120"),
               new XElement(initNs + "DrRefNmbr", "DEBITREF"),
               new XElement(initNs + "DrDesc", "DRDESC")
            });

        }
    }
}