G. T. G. T. - 2 years ago 156
C# Question

Converting the Xml tags

In my xml some of the xml element are empty for example:


So I want to modify the elements that are of the type shown above to convert to
type element so how can we do it in C# and update in the file.

Answer Source

Here's what I came up with:

private static void FormatEmptyNodes(XmlNode rootNode)
    foreach (XmlNode childNode in rootNode.ChildNodes)

        if(childNode is XmlElement)
            XmlElement element = (XmlElement) childNode;
            if (string.IsNullOrEmpty(element.InnerText)) element.IsEmpty = true;

Then use it like this:

var doc = new XmlDocument();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download