Reputation: 478
here is my XML doc
<Filters>
<Filter Name="ddd">
<File Name="filename.xls" Location="\\path\filename.xls">
<Sheet Name="'sheetname'">
<field Name="Pick up point" Condition="LIKE" Value="k" />
</Sheet>
</File>
</Filter>
<Filter Name=""ccc>
<File Name="filename.xls" Location="\\path\filename.xls">
<Sheet Name="'sheetname'">
<field Name="Pick up point" Condition="LIKE" Value="k" />
</Sheet>
</File>
</Filter>
<Filters>
now what i want is to have multiple field elements but this field element comes from a loop here is how i was making it
xml.Add(new XElement("Filter", new XAttribute("Name", getCriteriaName),
new XElement("File", new XAttribute("Name", getFileName), new XAttribute("Location", excelFileLocation),
new XElement("Sheet", new XAttribute("Name", getExcelSheetName),
new XElement("field", new XAttribute("Name", getExcelColumnName), new XAttribute("Value", getCondition))))));
xml.Save(fileLocation);
i changed the code like this
xml.Add(new XElement("Filter", new XAttribute("Name", getCriteriaName),
new XElement("File", new XAttribute("Name", getFileName), new XAttribute("Location", excelFileLocation),
new XElement("Sheet", new XAttribute("Name", getExcelSheetName))));
while (conditions.Rows.Count > rowCount)
{
xml.Add(new XElement("field", new XAttribute("Name", conditions.Rows[rowCount][0]),new XAttribute("Condition", conditions.Rows[rowCount][1]), new XAttribute("Value", conditions.Rows[rowCount][2])));
isSaved = true;
rowCount++;
}
xml.Save(fileLocation);
but it is making field after the filter tag closes how can i do it in the above format
Upvotes: 1
Views: 934
Reputation: 1502386
Well yes, you're calling xml.Add
when you actually want to call Add
on the object representing the Sheet
element.
I suggest you use:
XElement sheet = new XElement("Sheet", new XAttribute("Name", getExcelSheetName);
while (conditions.Rows.Count > rowCount)
{
sheet.Add(new XElement("field", ...));
isSaved = true; // Unclear what this is for...
rowCount++;
}
xml.Add(new XElement("Filter", new XAttribute("Name", getCriteriaName),
new XElement("File", ...),
sheet);
You may well also be able to express all those field
elements as a query:
XElement sheet = new XElement("Sheet", new XAttribute("Name", getExcelSheetName),
conditions.Rows.Select(row => ...));
Upvotes: 3