code_junkie

Convert CSV file or Excel spreadsheet to RESX File

Nov 11th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. namespace SampleResourceImport
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7.  
  8. XmlDocument doc = new XmlDocument();
  9. string filePath = @"[file path to your resx file]";
  10. doc.Load(filePath);
  11. XmlElement root = doc.DocumentElement;
  12.  
  13. XmlElement datum = null;
  14. XmlElement value = null;
  15. XmlAttribute datumName = null;
  16. XmlAttribute datumSpace = doc.CreateAttribute("xml:space");
  17. datumSpace.Value = "preserve";
  18.  
  19. // The following mocks the actual retrieval of your localized text
  20. // from a CSV or ?? document...
  21. // CSV parsers are common enough that it shouldn't be too difficult
  22. // to find one if that's the direction you go.
  23. Dictionary<string, string> d = new Dictionary<string, string>();
  24. d.Add("Label1", "First Name");
  25. d.Add("Label2", "Last Name");
  26. d.Add("Label3", "Date of Birth");
  27.  
  28. foreach (KeyValuePair<string, string> pair in d)
  29. {
  30. datum = doc.CreateElement("data");
  31. datumName = doc.CreateAttribute("name");
  32. datumName.Value = pair.Key;
  33. value = doc.CreateElement("value");
  34. value.InnerText = pair.Value;
  35.  
  36. datum.Attributes.Append(datumName);
  37. datum.Attributes.Append(datumSpace);
  38. datum.AppendChild(value);
  39. root.AppendChild(datum);
  40. }
  41.  
  42. doc.Save(filePath);
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment