Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Want to Create an Xml format through C# source code.

Posted on 2014-03-27
8
Medium Priority
?
310 Views
Last Modified: 2014-04-12
Want to generate XML though C#, how to achieve it.

<Message>
<Request>
<Detail>
<TranType>Credit</TranType>
<TranAction>Auth</TranAction>
<Amount>19.99</Amount>
<CurrencyCode>840</CurrencyCode>
</Detail>
<IndustryData>
<Industry>CardNotPresent</Industry>
<Eci>7</Eci>
</IndustryData>
<Account>
<Pan>4111111111111111</Pan>
<Expiration>1217</Expiration>
<Postal>197201234</Postal>
<Address>123 Fake Street</Address>
</Account>
</Request>
<Authentication>
<Client>73</Client>
<Source>1</Source>
</Authentication>
</Message>

Open in new window

0
Comment
Question by:Tapan Pattanaik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39958623
in which format is your input data?

check at the "Generate a XML output" section from http://emoreau.com/Entries/Articles/2009/04/Using-LINQ-and-XML-Literals-to-transform-a-DataTable-into-a-HTML-table.aspx
0
 
LVL 2

Expert Comment

by:graber
ID: 39958898
I've had to do this sort of thing with JSON.  From the question I'm spit balling here but I would created a class hierarchy to manage building the the message. The structure of XML lends itself to that nicely.  Each class<like message> would now the tags it would use to wrap itself as well as each field. An taking it in chunks helped me to work out the user interface.

<Message> cMessage
   <Request> cRequest
      <Detail> cDetail
         <TranType>Credit</TranType> member
         <TranAction>Auth</TranAction>member
         <Amount>19.99</Amount>
         <CurrencyCode>840</CurrencyCode>
      </Detail>
      <IndustryData> cIndustryData
         <Industry>CardNotPresent</Industry>
         <Eci>7</Eci>
      </IndustryData>
      <Account>
         <Pan>4111111111111111</Pan>
         <Expiration>1217</Expiration>
         <Postal>197201234</Postal>
         <Address>123 Fake Street</Address>
      </Account>
   </Request>
   <Authentication>
      <Client>73</Client>
      <Source>1</Source>
   </Authentication>
</Message>

Open in new window


The approach is fairly strait forward
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39959114
Please share with us the source of the input data and its format which will be used to create the XML document.
0
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 
LVL 21

Author Comment

by:Tapan Pattanaik
ID: 39996238
Hi experts,

from the method I am getting xml string like this  and I want to Keep inside "<Response>" how will I do that.

string clResponse = EncryptDecrypt.DecryptString(ClText, key, iv);

I am getting xml string like this

    <Reference>
        <Guid>3TKYN2H3FKN501J</Guid>
        <TranDate>04/11/2014</TranDate>
        <TranTime>12:18:58</TranTime>
    </Reference>
    <Result>
        <ResponseCode>00</ResponseCode>
        <ResponseText>Query Complete</ResponseText>
    </Result>
    <QueryResponse>
        <Row>
            <Client>177</Client>
            <Dba>Tarang</Dba>
            <Guid>3P9JFLVNFL4VMXN</Guid>
            <TranType>Credit</TranType>
            <TranAction>Sale</TranAction>
            <ResponseCode>00</ResponseCode>
            <ResponseText>Approved</ResponseText>
            <FirstName>John</FirstName>
            <LastName>Doe</LastName>
            <Amount>1.00</Amount>
            <LastFour>1111</LastFour>
            <CardType>Visa</CardType>
            <TranDate>02/16/2014</TranDate>
            <TranTime>11:13:07</TranTime>
            <Status>Settled</Status>
        </Row>
    </QueryResponse>

How I will I save it to a xml file. How will I achieve this ?? Please help me
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39996489
Hi  TAPAN;

The XML that you posted has no node with the name Response and also has no root node. Which part do you want to get?
0
 
LVL 21

Author Comment

by:Tapan Pattanaik
ID: 39996500
How will I add Root node as "<Response>"  in the above xml string which I am receiving from the client and I want to save the above xml file. File should be like this.

<Response>
 <Reference>......</Reference>
 <Result>......</Result>
<QueryResponse>.........</QueryResponse>
</Response>
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39996540
Hi TAPAN;

The following code snippet will do what you are looking for.

// Create the XML document declaration
XDocument xdoc = new XDocument(
                     new XDeclaration("1.0", "utf-8", "yes")
                 );

// Add the root node to the XML string data and create the XML elements 
XElement response = XElement.Parse("<Resppnse>" + clResponse + "</Resppnse>");
// Add all the elements to the document
xdoc.Add(response);
// If you want to save the XML to disk do the following
// changing the path and file name
xdoc.Save(@"C:\Working Directory\response.xml");

Open in new window

0
 
LVL 21

Author Closing Comment

by:Tapan Pattanaik
ID: 39996579
Thank you so much
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question