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

x
?
Solved

C# .NET code duplicate and refactore

Posted on 2016-08-30
6
Medium Priority
?
62 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
[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
  • 2
6 Comments
 
LVL 35

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 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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