Search code examples
c#.netxmlsharepointxelement

Xelement to expanded empty tags


I am generating XML in my c#, when I have few empty tags for example,

 new XElement("TransLogID", "")

some of those gets rendered as

<TransLogID></TransLogID>

while some of those gets rendered as

<TransLogID/>

What controls when the tags will be expanded and when not? How can I force them to be in a behavior I want?


Solution

  • I think they have different origins.

    Root.Add(new XElement("TransLogID1", ""));
    Root.Add(new XElement("TransLogID2"));
    

    will give

    <TransLogID1></TransLogID1>
    <TransLogID2/>
    

    Both elements will have empty Elements/Nodes collections, the subtle difference is that the TransLogID2 will have IsEmpty=true.