Solved

C# .NET code duplicate and refactore

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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