c# - XDocument formatting elements -


i have issue xdocument keep getting format:

<db caption="001" rules="6">     <rules>         <rule data_type="2" option="0">             <rule data_type="2" option="0"/>         </rule>     </rule> </db> 

here code:

new xelement("db", new xattribute("caption", "001"), new xattribute("rules","6"), new xelement("rule", new xattribute("data_type", "2"), new xattribute("option", "0"), new xelement("rule", new xattribute("data_types", "2"), new xattribute("option", "0"))); 

however need output in following format:

<db caption="001" rules="6">     <rules>         <rule data_type="2" option="0"/>         <rule data_type="2" option="0"/>     </rules> </db> 

----edit

i have tried:

new xelement("db", new xattribute("caption", "001"), new xattribute("rules","6"), new xelement("rule"), new xattribute("data_type", "2"), new xattribute("option", "0"), new xelement("rule"), new xattribute("data_types", "2"), new xattribute("option", "0")); 

the above didn't work either.

let's format code see what's happening:

new xelement("db",     new xattribute("caption", "001"),     new xattribute("rules","6"),     new xelement("rule",         new xattribute("data_type", "2"),         new xattribute("option", "0"),         new xelement("rule",             new xattribute("data_types", "2"),             new xattribute("option", "0"))); 

you creating second 'rule' element within first one. so, have root element 'db', has 'rule' child, has 'rule' child.

try instead:

new xelement("db",     new xattribute("caption", "001"),     new xattribute("rules","6"),     new xelement("rule",         new xattribute("data_type", "2"),         new xattribute("option", "0")),     new xelement("rule",         new xattribute("data_type", "2"),         new xattribute("option", "0"))); 

code readability goes long way in solving bugs.


Comments