Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 906
socialMatrix Avatar asked Dec 17 '22 07:12

socialMatrix


2 Answers

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.

like image 144
Henk Holterman Avatar answered Dec 31 '22 20:12

Henk Holterman


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?

like image 45
Steve Danner Avatar answered Dec 31 '22 18:12

Steve Danner