Guest User

Untitled

a guest
Oct 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. private XDocument MapSamiToXml(string p)
  2. {
  3. XDocument xml = new XDocument();
  4. var ws = new char[] { '\r', '\n' };
  5. var syncTag = new string[] { "<Sync " };
  6. var pTag = new string[] { "<P " };
  7.  
  8. // Slit up the input into sync groups
  9. var syncs = p.Split(syncTag, StringSplitOptions.None);
  10. List<XElement> xmlContent = new List<XElement>();
  11.  
  12. foreach (string s in syncs)
  13. { // process each sync group in turn
  14. if (s.Contains("Start="))
  15. {
  16. string attribs1 = s.Substring(0, s.IndexOf('>'));
  17. string rest = s.Remove(0, attribs1.Length + 1).TrimStart(ws);
  18. List<XElement> kids = new List<XElement>();
  19.  
  20. // process all the content between two <P> tags.
  21. foreach (string content in rest.Split(pTag, StringSplitOptions.RemoveEmptyEntries))
  22. {
  23. string attribs2 = content.Substring(0, content.IndexOf('>'));
  24. // extract any remaining plain text as the caption text
  25. string caption = RemoveHtml(content.Remove(0, attribs2.Length + 1).TrimStart(ws));
  26. XElement pElement = new XElement("P");
  27. AddXmlAttributes(pElement, attribs2);
  28. pElement.SetValue(caption);
  29. kids.Add(pElement);
  30. }
  31. XElement syncElement = new XElement("Sync",kids.ToArray());
  32. AddXmlAttributes(syncElement, attribs1);
  33. xmlContent.Add(syncElement);
  34. }
  35. }
  36. xml.Add(new XElement("Body", xmlContent.ToArray()));
  37. return xml;
  38. }
  39.  
  40. private static string RemoveHtml(string p)
  41. {
  42. string plaintext = p;
  43. plaintext = Regex.Replace(plaintext, @"<[^>]*>", String.Empty);
  44. plaintext = Regex.Replace(plaintext, @"&[^;]*;", String.Empty);
  45. return plaintext.Trim();
  46. }
  47.  
  48. private static void AddXmlAttributes(XElement pElement, string prefix)
  49. {
  50. var syntax = new char[] { ' ', '=' };
  51. var attribs = prefix.Split(syntax);
  52. int i = 0;
  53. while (i < attribs.Length)
  54. {
  55. pElement.SetAttributeValue(attribs[i], attribs[i + 1]);
  56. i += 2;
  57. }
  58. }
Add Comment
Please, Sign In to add comment