Mega Code Archive

 
Categories / C# / XML
 

Sets the inner text in a node If the node doesnt exist, it creates a new one and adds the text to it

using System; using System.Xml; namespace Microsoft.SnippetLibrary {     /// <summary>     /// Summary description for Util.     /// </summary>     public class Utility     {         /// <summary>         /// Sets the inner text in a node.  If the node doesn't         /// exist, it creates a new one and adds the text to it.         /// </summary>         /// <param name="element">The element.</param>         /// <param name="name">The name.</param>         /// <param name="text">The text.</param>         /// <param name="nsMgr">The ns MGR.</param>         /// <returns></returns>         public static XmlNode SetTextInDescendantElement(XmlElement element, string name, string text, XmlNamespaceManager nsMgr)         {             return SetTextInElement(element,name,text,nsMgr,false);         }         public static XmlNode SetTextInChildElement(XmlElement element, string name, string text, XmlNamespaceManager nsMgr)         {             return SetTextInElement(element, name, text, nsMgr, true);         }         private static XmlNode SetTextInElement(XmlElement element, string name, string text, XmlNamespaceManager nsMgr, bool isChild)         {             if (element == null)                 throw new Exception("Passed in a null node, which should never happen.");             var selector = "descendant";             if (isChild)                 selector = "child";             XmlElement newElement = (XmlElement)element.SelectSingleNode(selector + "::ns1:" + name, nsMgr);             if (newElement == null)             {                 newElement = (XmlElement)element.AppendChild(element.OwnerDocument.CreateElement(name, nsMgr.LookupNamespace("ns1")));             }             newElement.InnerText = text;             return element.AppendChild(newElement);         }     } }