Solved

C# .NET code duplicate and refactore

Posted on 2016-08-30
6
59 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 34

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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