Posts Tagged ‘ XmlHelper

Update XmlHelper v1.01

I’ve updated the code for XmlHelper by adding two methods that I had forgoten about untill I needed them this afternoon.

        /// <summary>
        /// Attractively format the XML with consistant indentation.
        /// </summary>
        /// <param name="strXML">A well formed XML string</param>
        /// <returns>An XML string with carage returns and indentations</returns>
        public static string PrettyPrint(string strXML)
 
        /// <summary>
        /// Attractively format the XML with consistant indentation.
        /// </summary>
        /// <param name="doc">The Xml Document you want to convert</param>
        /// <returns>An XML string with carage returns and indentations</returns>
        public static string PrettyPrint(XmlDocument doc)

both will take this :-

<?xml version="1.0" encoding="utf-8"?><NewDataSet><Foo><Bar>abc</Bar></Foo></NewDataSet>

to this :-

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <Foo>
    <Bar>abc</Bar>
  </Foo>
</NewDataSet>

You can find XmlHelper v1.01 under projects, as always if anyone wants to suggest other methods or if you are using it and find a bug let me know…

XmlHelper v1.00

I got fed up with rewriting the same methods over and over when I do any XML work so I put together a small helper class.

I present it here for anyone who wants it, I’m only releasing the DLL at the moment and it’s here under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License 

Once you have added a referance to the DLL add

using BHS.XML

and then you can call any of the methods, the class and it’s methods are static.

namespace BHS.XML
{
    /// <summary>
    /// Black Hat Software:XmlHelper
    /// A series of static methods to do common tasks with XML
    /// </summary>
    public static class XmlHelper
    {
        /// <summary>
        /// Adds the Named attribute with a given value to an existing Node
        /// </summary>
        /// <param name="AddTo">Node to add the new attribute</param>
        /// <param name="attribName">New attribute name</param>
        /// <param name="attribValue">Value to set</param>
        /// <returns>a link to the new attribute created</returns>
        public static XmlAttribute AddAttrib(XmlElement AddTo, string attribName, string attribValue)
 
        /// <summary>
        /// Read a value from a given attribute on an existing node
        /// </summary>
        /// <param name="ReadFrom">node that has the attribute</param>
        /// <param name="AttribName">Name of the Attribute</param>
        /// <returns>value of the attribute or NULL if not found</returns>
        public static string ReadAttrib(XmlElement ReadFrom, string AttribName)
 
        /// <summary>
        /// Create a new node on a given node
        /// </summary>
        /// <param name="AddTo">Node to add the new node to</param>
        /// <param name="NodeName">Name of the new node</param>
        /// <returns>A referance to the newly created node</returns>
        public static XmlElement AddNode(XmlElement AddTo, string NodeName)
 
        /// <summary>
        /// Create a new text node on a given node with a given value
        /// </summary>
        /// <param name="AddTo">Node to add the new node to</param>
        /// <param name="NodeName">Name of the new node</param>
        /// <param name="NodeValue">Text value to set the new node to</param>
        /// <returns>A referance to the newly created node</returns>
        public static XmlElement AddTextNode(XmlElement AddTo, string NodeName, string NodeValue)
 
        /// <summary>
        /// Read a value from a given text node on an existing node
        /// </summary>
        /// <param name="ReadFrom">Node with the node to read</param>
        /// <param name="NodeName">Name of the node to read the value</param>
        /// <returns>inner text of the node.</returns>
        public static string ReadTextNode(XmlElement ReadFrom, string NodeName)
    }
}

If anyone wants to suggest other methods or if you are using it and find a bug let me know…