I am trying to write to an xml file in a certain structure and am experiencing some issues. I want to write several XElements to a certain XElement in an XML. For ex:
foreach (XMLFieldInfo Field in XMLFields)
Fields.Add(new XElement("Field", new XElement("Name", Field.FieldID), new XElement("InclusionItems", Field.InclusionListToWriteToXML), new XElement("ExclusionItems", Field.ExclusionListToWriteToXML)));
Here i build several XElements i want to add as subnodes to another node in an xml.
XElement LockboxConfigTree = new XElement("Student", new XElement("ID", cmbID.Text.Trim()), .................);
The …… is where each element in the list created above will be added but i dont know how many elements there are so i cant find a way to add it to the supernode.
The above is the supernode i want to add the subnodes created above to. The problem is, i cant seem to get the right logic so in the end, the xml will look like this:
<Student>
<ID></ID>
<Field>
<Name></Name>
<InclusionList></InclusionList>
<ExclusionList></InclusionList>
</Field>
</Student>
The XElement constructor supports enumerable arguments.
You only have to pass your
Fieldsvariable:Actually, with LINQ, you don’t even need a
Fieldsvariable: