How to generate XML using XML document and Enumerable.Range?

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 809
I need to generate an xml as under

<DbRuleMappings>
<DbRuleMapping dbDetailId = "1" ruleMasterId="1" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="2" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="3" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="4" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="5" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="6" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="7" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="8" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="9" activeFlag="1"/>
<DbRuleMapping dbDetailId = "1" ruleMasterId="10" activeFlag="1"/>
</DbRuleMappings>


How to do so using XDocument?

XDocument docDBRuleMapping =
new XDocument(
new XElement("DbRuleMappings",
Enumerable.Range(1, 10).Select(x => x)
.Select(i => new XElement("DbRuleMapping",
new XAttribute("dbDetailId", "1"),
new XAttribute("ruleMasterId", i),
new XAttribute("activeFlag", 1)
))));

Comments or Responses

Login to post response