Solved

C# .NET code duplicate and refactore

Posted on 2016-08-30
6
36 Views
Last Modified: 2016-10-22
Hi

I have lot of code which needs to be refactored.  I want to reduce and do better the code.

I have attached the DTO class and SFCOntacts which has the model . It has methods which converts XMLDocument to SFContacts. This may need some better way. Can I improve.

The Consumer class also duplicate code for Create and Update. Can I clean this. This class calls service and invoke SFContacts methods for conversion

Thanks in Advance
SFContacts.cs
ConsumingClass.txt
0
Comment
Question by:Member_2_7967608
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Can you explain why you're fiddling with XML in the samples?
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
Comment Utility
Hey,
You can do it in much better way.

1. To work with XML it is always been good to work with XML model class.
    Means you can directly create a class and serialize it with just couple of lines.

Lets assume that, you have below XML

<TEST_XML_DOC>
   <USER_DETAILS>
	<ID>1<ID>
	<NAME>ABC<NAME>
	<ADDRESS>INDIA<ADDRESS>
   </USER_DETAILS>
   <USER_DETAILS>
	<ID>2<ID>
	<NAME>PQR<NAME>
	<ADDRESS>USA<ADDRESS>
   </USER_DETAILS>
</TEST_XML_DOC>

Open in new window


You can create a class for this XML by using command.
First open XML file in Visual Studio.
Then From XML Menu Click on Create Schema...
It will generate the .xsd file. Save it on the drive.
After this Open Visual Studio 2013 Tools Command Prompt and write the following command and hit enter
D:\> xsd TEST_XML_DOC.xsd /c

This will generate the .cs file for that XML document. Add that class file in your project.

To read the XML file just use below code.
Assuming that you are getting XML in byte array parameter

byte[] XMLFile;
TEST_XML_DOC test = null;
var doc = new XmlDocument();
doc.LoadXml(Encoding.Default.GetString(XMLFile));
var ser = new XmlSerializer(typeof(TEST_XML_DOC));
if (doc.DocumentElement != null)
   test = (TEST_XML_DOC)ser.Deserialize(new XmlNodeReader(doc.DocumentElement));

Open in new window


Now you will get the USER_DETAILS in test  object
To read this
var Users= test.USER_DETAILS;

Open in new window

Now you will get all the data in Users object

Hope this will help you !!
0
 

Author Comment

by:Member_2_7967608
Comment Utility
I will update with Sample Data
0
 
LVL 19

Expert Comment

by:Manoj Patil
Comment Utility
As per the question, I think I have provided the solution.
And the Author has not commented anything afterwards.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now