Xml Manipulation in C

An example for XML manipulation in C#, changing nodes, removing attributes etc.

public class XmlManipulator
{

    /// <summary>
    /// Remove an attribute from the XML document.
    /// </summary>
    /// <param name="xPath"></param>
    /// <param name="attributeName"></param>
    /// <returns></returns>
    public static bool RemoveAttribute(XmlDocument doc, String xPath, String attributeName)
    {
        try
        {
            var root = doc.FirstChild;
            XmlNode child = doc.SelectSingleNode(xPath);

            if (child.Attributes[attributeName] != null)
                child.Attributes.Remove(child.Attributes[attributeName]);
        }
        catch
        {
            return false;
        }

        return false;
    }

    /// <summary>
    /// Remove a node from the XML document.
    /// </summary>
    /// <param name="xPath"></param>
    /// <returns></returns>
    public static bool RemoveNode(XmlDocument doc, String xPath)
    {
        try
        {
            XmlNode node = doc.SelectSingleNode(xPath);

            if (node != null)
            {
                node.ParentNode.RemoveChild(node);
                return true;
            }
        }
        catch
        {
            return false;
        }

        return false;
    }


    /// <summary>
    /// removes attribute by xpath
    /// </summary>
    /// <param name="doc"></param>
    /// <param name="xPath"></param>
    /// <returns></returns>
    public static bool RemoveAttribute(XmlDocument doc, String xPath)
    {
        string[] stringSeparators = new string[] { "/./@" };
        string[] split = xPath.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);

        if (split.Length != 2) return false;

        try
        {
            XmlNode node = doc.SelectSingleNode(split[0]);

            if (node.Attributes[split[1]] != null)
            {
                node.Attributes.Remove(node.Attributes[split[1]]);
                return true;
            }

        } catch
        {
            return false;
        }

        return false;
    }


    public static bool SetAttribute(XmlDocument doc, String xPath, String value)
    {
        string[] stringSeparators = new string[] { "/@" };
        string[] split = xPath.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);

        if (split.Length != 2) return false;

        try
        {
            XmlNode node = doc.SelectSingleNode(split[0]);

            if (node.Attributes[split[1]] != null)
            {
                node.Attributes[split[1]].InnerText = value;
                return true;
            }

        }
        catch
        {
            return false;
        }

        return false;

    }


    /// <summary>
    /// sets a node content
    /// </summary>
    /// <param name="doc"></param>
    /// <param name="xPath"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool SetNode(XmlDocument doc, String xPath, String value)
    {
        try
        {
            XmlNode node = doc.SelectSingleNode(xPath);

            if (node != null)
            {
                node.InnerText = value;
                return true;
            }
        }
        catch
        {
            return false;
        }

        return false;
    }

}

Werbung



comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum