instead of if InnerText is null I am writing a C # class to write an XML file that need...">

Write XML element as <element / "> instead of <element> </element> if InnerText is null

I am writing a C # class to write an XML file that needs to match exactly the structure of the existing XML that we are using so that some legacy systems don't get confused.

When the InnerText value of an element is null, I need the element markup of the xml element to be like

<element/>

      

instead

<element></element>

      

I managed to do it by accident but could not repeat it. Here is an example of the method I am using to write the xml file:

public bool WriteXML(string path)
    {
        // Create the xml document in memory inc. xml declaration
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

        // Create the root element
        doc.AppendChild(dec);
        XmlElement rootNode = doc.CreateElement("root");
        doc.AppendChild(rootNode);

        // Create elements at root node
        XmlElement XE_level1 = doc.CreateElement("level1");
        XE_level1.InnerText = "Text";
        rootNode.AppendChild(XE_level1);

        // Create a user data element
        XmlElement XE_level2 = doc.CreateElement("level2");
        XE_level2.InnerText = "Text";
        XE_level1.AppendChild(XE_level2);

        doc.Save(path);
    }

      

+3


source to share


4 answers


An XmlElement

seems to be expanded on every call to a set property attribute InnerText

. This piece of code works for new XmlElement

s:



if(text != null) {
    myNode.InnerText = text;
}

      

+3


source


As @Binkan Salaryman stated there is no need to set it IsEmpty

explicitly, this is true by default . Just don't install InnerText

when you don't need it. You can write a simple extension method if you need this behavior in many places:

public static class XmlUtils
{
    public static void SetInnerText(this XmlElement xmlElement, string text)
    {
        if(text != null)
            xmlElement.InnerText = text;
    }
}

      



And use it like this:

// text can be null here, element will still looks like <level2 />
XE_level2.SetInnerText(text); 

      

+3


source


What you want seems to be XmlElement.IsEmpty ( https://msdn.microsoft.com/en-us/library/system.xml.xmlelement.isempty(v=vs.110).aspx )

The docs say:

Returns true if the element should be serialized as short tags " <item/>

"; false for long format " <item></item>

".

When this property is set, if set to true, the children of this element are removed and the element is serialized in short tag format.

+1


source


If there is no value for the element, set the IsEmpty property to true and this will give you the desired result.

   public static void WriteXML(string path)
    {
        // Create the xml document in memory inc. xml declaration
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

        // Create the root element
        doc.AppendChild(dec);
        XmlElement rootNode = doc.CreateElement("root");
        doc.AppendChild(rootNode);

        // Create elements at root node
        XmlElement XE_level1 = doc.CreateElement("level1");
        XE_level1.InnerText = "Text";
        rootNode.AppendChild(XE_level1);

        // Create a user data element

        string s = String.Empty;

        if (String.IsNullOrEmpty(s))
        {
            XmlElement XE_level2 = doc.CreateElement("level2");
            XE_level2.IsEmpty = true;
            XE_level1.AppendChild(XE_level2);
        }
        else
        {
            XmlElement XE_level2 = doc.CreateElement("level2");
            XE_level2.InnerText = s;
            XE_level1.AppendChild(XE_level2);
        }



        doc.Save(path);


    }

      

0


source







All Articles