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
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…