And here it is:

<project>
  <script language="C#" prefix="xml">
    <imports>
      <import namespace="System.Xml"/>
    </imports>
    <code>
      <![CDATA[
        [Function("value-exists")]
        public static bool DoesXmlValueExist(string _xmlFile, string _xPath)
        {
          XmlDocument xd = new XmlDocument();
          xd.Load(_xmlFile);
          try
          {
            string s = xd.SelectSingleNode(_xPath).Value;
          }
          catch (NullReferenceException)
          {
            return false;
          }
          return true;
        }
       
        [Function("count")]
        public static int CountNodes(string _xmlFile, string _xPath)
        {
          try
          {
            XmlDocument xd = new XmlDocument();
            xd.Load(_xmlFile);
            return xd.SelectNodes(_xPath).Count;
          }
          catch(Exception err)
          {
            Console.WriteLine("Error (" + err.Message + ") getting match count of query " + _xPath);
            throw err;
          }
        }
       
        [Function("get-value")]
        public static string GetXmlValue(string _xmlFile, string _xPath)
        {
          try
          {
            XmlDocument xd = new XmlDocument();
            xd.Load(_xmlFile);
            return xd.SelectSingleNode(_xPath).Value;
          }
          catch(Exception err)
          {
            Console.WriteLine("Error (" + err.Message + ") running getting value of query " + _xPath);
            throw err;
          }
        }

        [Function("get-xml")]
        public static string GetXmlAsString(string _xmlFile, string _xPath)
        {
          try
          {
            XmlDocument xd = new XmlDocument();
            xd.Load(_xmlFile);
            return xd.SelectSingleNode(_xPath).InnerXml;
          }
          catch(Exception err)
          {
            Console.WriteLine("Error (" + err.Message + ") running getting xml of query " + _xPath);
            throw err;
          }
        }

        [Function("get-value-without-failing")]
        public static string GetXmlValueWithoutFailing(string _xmlFile, string _xPath)
        {
          string s;
          try
          {
            XmlDocument xd = new XmlDocument();
            xd.Load(_xmlFile);
            s = xd.SelectSingleNode(_xPath).Value;
          }
          catch
          {
            s = "";
          }
          return s;
        }
      ]]>
    </code>
  </script>
</project>

I put this into a file called xmlfunctions.incbuild which I <include> into my main project build scripts.  You can use the functions as normal like this:

<property name="my-xml-value" value="xml::get-value(filepath, xpath)"/>

John

On 12/01/06, John Ludlow <[EMAIL PROTECTED]> wrote:
I also have some functions in a <script> element that can perform various tasks on XML - get the number of matches to a certain query, get the XML children of a certain element, get the value of an XML node (basically like the peek task but using the ${x} syntax, which can be a bit cleaner).  I'm at home at the mo, but when I get to work, I'll post the code for it.  Nothing clever, really, just darn useful.

Reply via email to