// Test the appending of text data. public void TestXmlTextAppendData() { // Test simple appending. XmlText text = doc.CreateTextNode("hello"); RegisterWatchNeither(text); text.AppendData(" and goodbye"); CheckForChangeNeither(); AssertEquals("AppendData (1)", 17, text.Length); AssertEquals("AppendData (2)", "hello and goodbye", text.Data); // Test event handling. RegisterWatchBoth(text); text.AppendData("blah"); CheckForChangeBoth(); RegisterWatchBefore(text); text.AppendData("blah2"); CheckForChangeBefore(); RegisterWatchAfter(text); text.AppendData("blah3"); CheckForChangeAfter(); AssertEquals("AppendData (3)", "hello and goodbyeblahblah2blah3", text.Data); }
public override void WriteString(string data) { CheckState(); if (current == null) { throw new InvalidOperationException("Current state is not acceptable for Text."); } if (attribute != null) { attribute.AppendChild(doc.CreateTextNode(data)); } else { XmlText last = current.LastChild as XmlText; if (last == null) { current.AppendChild(doc.CreateTextNode(data)); } else { last.AppendData(data); } } }
public static void Normalise(XmlNode n) { XmlText prev = null; ArrayList deletedNodes = new ArrayList(); foreach (XmlNode child in n.ChildNodes) { XmlText t = child as XmlText; if (t == null) { prev = null; continue; } if (prev != null) { prev.AppendData(t.Data); deletedNodes.Add(t); } else { prev = t; } } foreach (XmlNode c in deletedNodes) { c.ParentNode.RemoveChild(c); } }
/// <summary> /// Dadas 2 revisões, realiza o processo de unificação entre elas, caso elas sejam siblings /// </summary> /// <param name="r1">Primeira revisão</param> /// <param name="r2">Segunda revisão</param> private static void UnificaRevisoes(XmlNode r1, XmlNode r2) { //Determina qual node aparece antes, se o r1 ou o r2. Isso porque a tag que vem depois será removida da árvore, //enquanto que o seu conteúdo será acrescentado à tag que vem primeiro. XmlNode antes, depois; //Garante que eles são siblings if (r1.NextSibling == r2 || r2.NextSibling == r1) { bool EmOrdem = r1.NextSibling == r2; antes = (EmOrdem ? r1 : r2); depois = (EmOrdem ? r2 : r1); } else { throw new Exception("Não é possível unificar as tags informadas porque elas não são sibblings"); } //Junta os textos XmlNode WTAntes = antes.FirstChild; XmlText TextoAntes = (XmlText)WTAntes.FirstChild; XmlText TextoDepois = (XmlText)depois.FirstChild.FirstChild; TextoAntes.AppendData(TextoDepois.Data); //Verifica se o atributo xml:space=preserve na tag w:t deve ser acrescentado, mantido ou eliminado const string ATTR_XML_SPACE = "xml:space"; bool Existe = WTAntes.Attributes.GetNamedItem(ATTR_XML_SPACE) != null; bool DeveExistir = (TextoAntes.Data.StartsWith(" ") || TextoAntes.Data.EndsWith(" ")); if (DeveExistir && !Existe) { ((XmlElement)WTAntes).SetAttribute(ATTR_XML_SPACE, "preserve"); } else if (!DeveExistir && Existe) { WTAntes.Attributes.RemoveNamedItem(ATTR_XML_SPACE); } //Elimina o segundo node (pois ele foi unificado com o primeiro depois.ParentNode.RemoveChild(depois); }