XML XElement and loops

本文关键字:loops and XElement XML | 更新日期: 2023-09-27 18:26:16

请帮助我修复导致此xml输出的代码段。

被称为"频率"answers"日程类型"的元素都被困在日程的第一个元素下。我该如何解决这个问题?

我从CSV类型的文件中读取数据,然后根据位置和值将相关数据(或转换后的值)添加到相关的XML值中。

您会看到,我在可能的情况下使用了建议的方法,但由于我必须使用额外的CASE语句,我陷入了困境,所以我恢复使用LAST语句,试图找到正确的元素来继续。

  <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;

XML XElement and loops

这句话就是问题所在:

test.Element("NBUConfig")
    .Element("Policies")
    .Elements("Policy").Last()
    .Elements("Schedules").Last()
    .Element("Schedule")
    .Add(new XElement(("SchedulesStuff"), (scount)));

您正在将SchedulesStuff添加到最后一个Schedules元素中的第一个Schedule元素。。。而不是将其添加到仅Schedules元素内的最后一个Schedule元素中。

如果您的重复代码要少得多,那么所有这些代码都会简单得多,也不容易出错。所以考虑一下:

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));        
}