socialMatrix
socialMatrix

Reputation: 1443

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?

Upvotes: 5

Views: 3172

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273219

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.

Upvotes: 10

Steve Danner
Steve Danner

Reputation: 22158

If your content is an empty string (new XElement("TransLogID", "")), it will render as

<TransLogID></TransLogID>

But if it is null (new XElement("TransLogID", null)), it will render as

<TransLogID/>

Are you sure you're always generating the nodes the same way?

Upvotes: 6

Related Questions