7
\$\begingroup\$

I have a method(C#) which takes an XmlNodeList and a String and saves the xml data into a database. That code isn't the problem (at least for the moment), but because I have multiple XmlNodeLists, each needing a different String (the heading), I end up with code like

saveToDB(students, "Students");
saveToDB(teachers, "Teachers");
saveToDB(workers, "Workers");

...etc, there's about 8 of those in total. It all works okay, but I wonder if there's a better way to call the function, rather than doing it 8 times? I guess I could wrap all of the data into an object, but I'd still have to set it up, and pull the data out before and afterwards, so that's surely just moving the problem elsewhere? Or is there simply no real way around this, and the issue lies with how I've approached this method in the first place?

I look forward to your responses.

asked Nov 7, 2012 at 9:15
\$\endgroup\$
3
  • \$\begingroup\$ Where does the saveToDB method live? Is it static? How are the XmlNodeList created and modified? \$\endgroup\$ Commented Nov 7, 2012 at 11:00
  • \$\begingroup\$ @Tag The saveToDB method is in the same class file. The XmlNodeList are created using the .SelectNodes method from a XmlElement object. \$\endgroup\$ Commented Nov 7, 2012 at 11:07
  • 1
    \$\begingroup\$ Oops, I asked 'How' when I meant to ask 'Where'. I was trying to understand the relationship between the node lists and the class where saveToDB lives. There might be a better way to organize the class to make saving the node lists easier. \$\endgroup\$ Commented Nov 7, 2012 at 12:16

2 Answers 2

7
\$\begingroup\$

I think you could create a new saveToDB method that requires a Dictionary<string, XmlNodeList> inside this new method you could cycle through the Keys (string) and for each one call the current saveToDB(string, XmlNodeList).

Yes, it only move your problem: you've to prefill the dictionary, but I really think there's no other options... at one point you HAVE to enumerate your datas.

answered Nov 7, 2012 at 11:07
\$\endgroup\$
3
  • \$\begingroup\$ I would agree with you, @tanathos, I think this is the best option. It'll reduce it down to a single looped call to saveToDB, and I doubt that it would effect performance noticeably. Thanks! \$\endgroup\$ Commented Nov 7, 2012 at 14:35
  • 3
    \$\begingroup\$ Just remember that a Dictionary doesn't preserve order; that might be okay, or you might need to use a List<KeyValuePair> instead. \$\endgroup\$ Commented Nov 8, 2012 at 22:55
  • \$\begingroup\$ It's a good point \$\endgroup\$ Commented Nov 8, 2012 at 23:23
1
\$\begingroup\$

There are few ways you can save our work .. this is one way you can do this easily..

you can have a List or Tuple of above string and XMLnodelist which you can iterate through and save the db result.

//pseudo code will be 
for(obj in collectionOfStringandXMlnodeList)
 SaveToDb(obj);
svick
24.5k4 gold badges53 silver badges89 bronze badges
answered Nov 7, 2012 at 11:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.