Solved

C# .NET code duplicate and refactore

Posted on 2016-08-30
6
45 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 33

Expert Comment

by:ste5an
ID: 41778036
Can you explain why you're fiddling with XML in the samples?
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 41778039
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
ID: 41788779
I will update with Sample Data
0
 
LVL 19

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
looking for an ASP.net report designer 4 41
Amazon S3 Images with .Net 3 29
Json and ajax 1 18
Convert VB web project to C# 3 32
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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