i have xml written following:
<arrayofproductline>     <productline>         <name>crm</name>         <actionfields>             <actionfield id="1">                 <name>a2</name>             </actionfield>             <actionfield id="2">                 <name>a1</name>             </actionfield>         </actionfields>         <processsteps>             <processstep>                 <name>marketing</name>                 <learningobjectives>                     <learningobjective actionfieldid="1">                         <paragraphs>                             <paragraph allowselection="false">                                 <text>lern ziel2</text>                                 <id>1</id>                             </paragraph>                             <paragraph allowselection="false">                                 <text>test</text>                                 <id>4</id>                             </paragraph>                         </paragraphs>                     </learningobjective>                     <learningobjective actionfieldid="2">                         <paragraphs>                             <paragraph allowselection="false">                                 <text>lern ziel2.1</text>                                 <id>2</id>                             </paragraph>                         </paragraphs>                     </learningobjective>                 </learningobjectives>             </processstep>             <processstep>                 <name>vertrieb</name>                 <learningobjectives>                     <learningobjective actionfieldid="1">                         <paragraphs>                             <paragraph allowselection="false">                                 <id>3</id>                             </paragraph>                         </paragraphs>                     </learningobjective>                 </learningobjectives>             </processstep>         </processsteps>     </productline> </arrayofproductline>   i want read xml , count maximum of learningobjective nodes , write empty learningobjective nodes in learningobjectives node if number of learningobjective nodes less maximum number of learning objective nodes using linq xml. quite new linq. can 1 please me changing xml.
okay, should split bit:
- loading xml
 - finding 
learningobjectivescontainer elements - counting 
learningobjectiveelements within each container - finding maximum value of counts
 - adding empty elements necessary
 
the first 4 pretty straightforward:
xdocument doc = xdocument.load("data.xml"); var containers = doc.descendants("learningobjectives").tolist(); var containercounts = containers.select(x => x.elements("learningobjectives")                                               .count()); var maxcount = containercounts.max();   i quite possibly have combined last 2 statements, wanted separate them out clarity in answer.
for final part, suggest not using linq - it's not fit mutations. need @ containers, , add right number of elements each one:
foreach (var container in containers) {     // same approach counting before.     int count = x => x.elements("learningobjectives").count()     (int = count; < maxcount; i++)     {         container.add(new xelement("learningobjective"));     } }      
Comments
Post a Comment