Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private XDocument MapSamiToXml(string p)
- {
- XDocument xml = new XDocument();
- var ws = new char[] { '\r', '\n' };
- var syncTag = new string[] { "<Sync " };
- var pTag = new string[] { "<P " };
- // Slit up the input into sync groups
- var syncs = p.Split(syncTag, StringSplitOptions.None);
- List<XElement> xmlContent = new List<XElement>();
- foreach (string s in syncs)
- { // process each sync group in turn
- if (s.Contains("Start="))
- {
- string attribs1 = s.Substring(0, s.IndexOf('>'));
- string rest = s.Remove(0, attribs1.Length + 1).TrimStart(ws);
- List<XElement> kids = new List<XElement>();
- // process all the content between two <P> tags.
- foreach (string content in rest.Split(pTag, StringSplitOptions.RemoveEmptyEntries))
- {
- string attribs2 = content.Substring(0, content.IndexOf('>'));
- // extract any remaining plain text as the caption text
- string caption = RemoveHtml(content.Remove(0, attribs2.Length + 1).TrimStart(ws));
- XElement pElement = new XElement("P");
- AddXmlAttributes(pElement, attribs2);
- pElement.SetValue(caption);
- kids.Add(pElement);
- }
- XElement syncElement = new XElement("Sync",kids.ToArray());
- AddXmlAttributes(syncElement, attribs1);
- xmlContent.Add(syncElement);
- }
- }
- xml.Add(new XElement("Body", xmlContent.ToArray()));
- return xml;
- }
- private static string RemoveHtml(string p)
- {
- string plaintext = p;
- plaintext = Regex.Replace(plaintext, @"<[^>]*>", String.Empty);
- plaintext = Regex.Replace(plaintext, @"&[^;]*;", String.Empty);
- return plaintext.Trim();
- }
- private static void AddXmlAttributes(XElement pElement, string prefix)
- {
- var syntax = new char[] { ' ', '=' };
- var attribs = prefix.Split(syntax);
- int i = 0;
- while (i < attribs.Length)
- {
- pElement.SetAttributeValue(attribs[i], attribs[i + 1]);
- i += 2;
- }
- }
Add Comment
Please, Sign In to add comment