Please help me to fix the piece of code that results in this xml output.
The elements called "Frequency" and "ScheduleType" are all being stuck under the first element of schedule. How do I fix this?
I'm reading data from a CSV type file and then based on the position and value I add the relevant data (or converted values) to the relevant XML values.
You'll see that I use the suggested method where possible but as I have to use an additional CASE statement I get stuck so I revert to using the LAST statement to try and find the correct element to continue from.
<Schedules>
<Schedule Number="1">
<ScheduleName>Cumulative</ScheduleName>
<BackupType>Cinc</BackupType>
<Multiplexing>4</Multiplexing>
<RetentionLevel>infinite</RetentionLevel>
<ResidenceIsSLP>No</ResidenceIsSLP>
<MultipleCopies>No</MultipleCopies>
<ScheduleType />
<Frequency>
<Count>5</Count>
<Unit>Days</Unit>
<Count>2</Count>
<Unit>Weeks</Unit>
<Count>1</Count>
<Unit>Days</Unit>
</Frequency>
<ScheduleType />
<Frequency />
<ScheduleType />
<Frequency />
</Schedule>
<Schedule Number="2">
<ScheduleName>Full</ScheduleName>
<BackupType>Full</BackupType>
<Multiplexing>4</Multiplexing>
<RetentionLevel>infinite</RetentionLevel>
<ResidenceIsSLP>No</ResidenceIsSLP>
<MultipleCopies>No</MultipleCopies>
</Schedule>
<Schedule Number="3">
<ScheduleName>Differential</ScheduleName>
<BackupType>Diff</BackupType>
<Multiplexing>4</Multiplexing>
<RetentionLevel>2 weeks</RetentionLevel>
<ResidenceIsSLP>No</ResidenceIsSLP>
<MultipleCopies>No</MultipleCopies>
</Schedule>
</Schedules>
public XDocument PolicyAttributesSection()
{
XDocument policies = XDocument.Load("C:\\Output Files\\testnbu.xml");
policies.Element("NBUConfig").Add(new XElement("Policies"));
using (TextFieldParser policy = new TextFieldParser("C:\\Input Files\\RAM\\bppllist.txt"))
{
string[] ignoreLines1 = { "NAMES", "KEY", "RCMD", "BCMD", "FOE", "SHAREGROUP", "APPLICATIONDEFINED", "SCHEDFOE" };
policy.TextFieldType = FieldType.Delimited;
policy.Delimiters = new string[] { " ", ",", ";" };
policy.HasFieldsEnclosedInQuotes = true;
policy.CommentTokens = ignoreLines1;
policy.TrimWhiteSpace = true;
string[] policyprops;
int schedulenumber;
while (!policy.EndOfData)
{
schedulenumber = 0;
var pl = policies.Element("NBUConfig").Element("Policies").Elements("Policy");
policyprops = policy.ReadFields();
switch (policyprops[0])
{
case "SCHED":
schedulenumber++;
int scheduletypenumber;
bool schedparsed = int.TryParse((policyprops[2]), out scheduletypenumber);
string scheduletypename = ScheduleType_Lookup[(scheduletypenumber)];
int retlevel;
bool retparsed = int.TryParse((policyprops[5]), out retlevel);
string retentionperiod = (Retention_Lookup[(retlevel)]).ToString();
int isslp;
bool isslpparsed = int.TryParse((policyprops[17]), out isslp);
int copynumber;
bool copynumberparsed = int.TryParse((policyprops[12]), out copynumber);
if (copynumber > 0)
{
copynumber = 1;
}
pl.Last().Element("Schedules").Add(new XElement(("Schedule"), new XAttribute("Number", (schedulenumber)),
(new XElement("ScheduleName", policyprops[1])),
(new XElement("BackupType", (scheduletypename))),
(new XElement("Multiplexing", policyprops[3])),
(new XElement("RetentionLevel", (retentionperiod))),
(new XElement("ResidenceIsSLP", (yesno_lookup[(isslp)]))),
(new XElement("MultipleCopies", (yesno_lookup[(copynumber)])))));
pl.Last().Element("Schedules").Element("Schedule").Add(new XElement(("ScheduleType")));
switch (policyprops[11])
{
case "1":
pl.Last().Element("Schedules").Element("Schedule").Add(new XElement("Calendar", "Enabled"));
pl.Last().Element("Schedules").Element("Schedule").Add
(new XElement("CalendarRetries", "Disabled"));
break;
case "2":
pl.Last().Element("Schedules").Element("Schedule").Add
(new XElement("Calendar", "Enabled"));
pl.Last().Element("Schedules").Element("Schedule").Add
(new XElement("CalendarRetries", "Enabled"));
break;
case "0":
int freqseconds;
bool startparsed = int.TryParse((policyprops[4]), out freqseconds);
var freq = frequency(freqseconds);
pl.Last().Element("Schedules").Element("Schedule").Add
(new XElement("Frequency"));
int count = freq.Item1;
string unit = freq.Item2;
pl.Last().Element("Schedules").Element("Schedule").Element("Frequency").Add
(new XElement("Count", (count).ToString()));
pl.Last().Element("Schedules").Element("Schedule").Element("Frequency").Add
(new XElement("Unit", (unit)));
break;
}
break;
}
}
policies.Save("C:\\Output Files\\testnbu.xml");
return policies;
This statement is the problem:
test.Element("NBUConfig")
.Element("Policies")
.Elements("Policy").Last()
.Elements("Schedules").Last()
.Element("Schedule")
.Add(new XElement(("SchedulesStuff"), (scount)));
You're adding the SchedulesStuff
to the first Schedule
element within the last Schedules
element... instead of adding it to the last Schedule
element within the only Schedules
element.
All of this code would be much, much simpler - and less error-prone - if you had far less repetitive code. So consider instead:
var policies = new XElement("Policies");
var test = new XDocument(new XElement("NBUConfig", policies));
// Hint: for loops are more idiomatic than while loops here
for (int pcount = 0; pcount < 3; pcount++)
{
var schedules = new XElement("Schedules");
for (int scount = 0; scount < 3; scount++)
{
schedules.Add(new XElement("Schedule",
new XAttribute("Number", scount),
new XElement("ScheduleStuff", scount));
}
policies.Add(new XElement("Policy",
new XElement("PolicyName", pcount),
new XElement("General"),
new XElement("Clients"),
new XElement("IncludeList"),
schedules));
}
See more on this question at Stackoverflow