using System;
using System.Xml;
namespace Microsoft.SnippetLibrary
{
///
/// Summary description for Util.
///
public class Utility
{
///
/// Sets the inner text in a node. If the node doesn't
/// exist, it creates a new one and adds the text to it.
///
/// The element.
/// The name.
/// The text.
/// The ns MGR.
///
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);
}
}
}