Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

Let me says that I have Mapping.xml file like this
XML
<ColumnName Mapping="HELLO LOOK FOR ME 1">COLUMNVALUE1</ColumnName>
<ColumnName Mapping="HELLO LOOK FOR ME 2">COLUMNVALUE2</ColumnName>



And I have a Data.csv file like this:
"HELLO LOOK FOR ME 1", "HELLO LOOK FOR ME 2", "I AM NOT IN MAPPING 1" ,"I AM NOT IN MAPPING 2"
467898, 4786, 222323, 4687327


Requirement is
1) Write a C# Code to Parse the Mapping.xml
2) Map the COLUMNNAME to the Value, Against Mapping attribute defined in the XML. In our case "HELLO LOOK FOR ME 1"

So the End Result should be having a C# Object holding the values.
COLUMNVALUE1 = 467898
COLUMNVALUE2 = 4786



3) Latter if I wish to Included Third Column, I have to just edit the XML File Mapping.xml
XML
<ColumnName Mapping="HELLO LOOK FOR ME 1">COLUMNVALUE1</ColumnName>
<ColumnName Mapping="HELLO LOOK FOR ME 2">COLUMNVALUE2</ColumnName>
<ColumnName Mapping="I AM NOT IN MAPPING 1">COLUMNVALUE3</ColumnName>


So that after the modification I should have a Object in C# having value

COLUMNVALUE1 = 467898
COLUMNVALUE2 = 4786
COLUMNVALUE3 = 222323


Is this possible with C#
Kindly note, I know how to parse read/write an XML file and also Parse the CSV Files. via C#
But the problem, here is I am looking for a Technique in C# that would suit my requirements.

If anyone know or have some idea kindly let me know

Thanks and Regards
Durai Velan C
Posted
Updated 7-Sep-15 14:31pm
v4
Comments
Maciej Los 7-Sep-15 17:03pm    
What have you tried till now? Where are you stuck?

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

Yes it is possible in C#, just like about any other language.

Otherwise, show us what you have done and explain where is your problem, you may get help.
 
Share this answer
 
v2
Comments
Duraivelan Chockalingam 8-Sep-15 4:25am    
Hello All,

Apologize for not being clear.

Yes infact, if you look at the question it could feel to be simple.

And at present I resolving this via the following method.

Having a Hash in C# and building the code to read the XML File, and using regex on Mapping Attribute to sort the value corresponding to the column from the CSV file.

HASH["COLUMNVALUE1"] = 467898
HASH["COLUMNVALUE2"] = 4786

Yes, I know this is simple.

But What I am expecting was to completely make this piece of module controlled externally.

Means, every time there is a new "COLUMNVALUE" I have to edit the C# code to include this variable in the C# and recompile it

So the expectation is

Read the XML Element Values Such as "COLUMNVALUE1" / "COLUMNVALUE2"
As C-Sharp Variable

Example:
String COLUMNVALUE1;
String COLUMNVALUE2;

And Get those String value from Parsing the CSV files,
via the Mapping Attribute that it had got from XML file

Or more preferably

Class ColumnNameClass {
Public String COLUMNVALUE1;
Public String COLUMNVALUE2;
}

ColumnNameClass Object = new ColumnNameClass();

Object.COLUMNVALUE1 = "Value Parse from CSV, based on its Mapping attribute"
Object.COLUMNVALUE2 = "Value Parse from CSV, based on its Mapping attribute"

And do what ever you want with that "Object"

Basically, I want this C# Module to be dynamic, and once complied I would not need to recompile it again

Until, if I want to change the Logic on how to treat the "Object" and it "Values"

Hope I have explained it properly.
And apologize still I am not clear enough
Further, I am just wondering how to treat String value from XML files, as Variable within C# code.

Further, I would like to do my Homework and not expecting someone to do it,
but just need some-hints or best-way on how to proceed with my expectation.

Thanks and Regards
Durai Velan C
Patrice T 8-Sep-15 12:37pm    
Ratherv than using a variable for each value, have tryed an array ?
and rather than repaeting the same piece of code, you should try to use a loop to feed the array, so that 1 piece of code feed the array with all values.
Duraivelan Chockalingam 9-Sep-15 8:44am    
There could be multiple ways of attaining it. Like using arrays, Hash and other.
But I need it the way that I expect. Because the

Class ColumnNameClass {
Public String COLUMNVALUE1;
Public String COLUMNVALUE2;
}

Would be a Nhibernate DBMapping Class.
Actually, I am looking to dump the Information back to DB after processing it.
Patrice T 9-Sep-15 16:04pm    
Hi,
I think you should repost the question with on what you have done, where you have a problem and why.
Explain that you get the number of columns bt reading an xml file, and where you are stuck.
My guess is that you started with something wrong from the beginning.
Make it clear that you are not requesting someone to make your homework by explaining what you have done and showing code. This way you more likely to get answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900