Example #1
0
        private static void ConvertElementContentToEntities(XmlDocument xmlDocument, EntityMapDictionary entityTranslate, XmlElement element)
        {
            if (element.HasAttributes)
            {
                foreach (XmlAttribute attribute in element.Attributes)
                {
                    if (attribute.Value.Length > 0)
                    {
                        ConvertTextNodesToEnties(xmlDocument, entityTranslate, attribute);
                    }
                }
            }

            if (element.IsEmpty)
            {
                return;
            }

            ConvertTextNodesToEnties(xmlDocument, entityTranslate, element);

            for (XmlNode currentNode = element.FirstChild; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is XmlElement)
                {
                    ConvertElementContentToEntities(xmlDocument, entityTranslate, (XmlElement)currentNode);
                }
            }
        }
Example #2
0
 private static void ConvertTextNodesToEnties(XmlDocument xmlDocument, EntityMapDictionary entityTranslate, XmlNode parentNode)
 {
     for (XmlNode currentNode = parentNode.FirstChild; currentNode != null; currentNode = currentNode.NextSibling)
     {
         if (currentNode is XmlText)
         {
             string text = currentNode.InnerText;
             for (int i = 0; i < text.Length; i++)
             {
                 char c = text[i];
                 if (entityTranslate.ContainsKey(c))
                 {
                     if (i > 0)
                     {
                         if (i == text.Length - 1)
                         {
                             XmlNode nextNode = parentNode.InsertAfter(xmlDocument.CreateEntityReference(entityTranslate[c]), currentNode);
                             currentNode.InnerText = text = currentNode.InnerText.Substring(0, i);
                             XmlNode node = currentNode;
                             if (ReferenceEquals(currentNode = currentNode.NextSibling, nextNode))
                             {
                                 break;
                             }
                             currentNode = node;
                         }
                         else
                         {
                             parentNode.InsertBefore(xmlDocument.CreateTextNode(text.Substring(0, i)), currentNode);
                             parentNode.InsertBefore(xmlDocument.CreateEntityReference(entityTranslate[c]), currentNode);
                             currentNode.InnerText = text = currentNode.InnerText.Substring(i + 1);
                         }
                         i = -1;
                     }
                     else if (text.Length == 1)
                     {
                         XmlNode nextNode = xmlDocument.CreateEntityReference(entityTranslate[c]);
                         parentNode.ReplaceChild(nextNode, currentNode);
                         currentNode = nextNode;
                         break;
                     }
                     else
                     {
                         parentNode.InsertBefore(xmlDocument.CreateEntityReference(entityTranslate[c]), currentNode);
                         currentNode.InnerText = text = currentNode.InnerText.Substring(1);
                         i--;
                     }
                 }
             }
         }
     }
 }