-1

私は2つのクラスを持っています

class WarningClass
{
        public string SqlEyeWarning { get; set; }
        public string FileName { get; set; }
}

class RemarkClass
{
        public string SqlEyeRamark { get; set; }
        public string FileName { get; set; }
}

それらは以下のように入力されます

List<WarningClass> lstWarningClass = new List<WarningClass>();
lstWarningClass.Add(new WarningClass { FileName = "a.sql", SqlEyeWarning = "SD001: Set QuotedIdentifier ON statement is missing" });
lstWarningClass.Add(new WarningClass { FileName = "b.sql", SqlEyeWarning = "SD001: Set QuotedIdentifier ON statement is missing" });
lstWarningClass.Add(new WarningClass { FileName = "c.sql", SqlEyeWarning = "SD009: Missing or order mismatch of Grant statement" });
lstWarningClass.Add(new WarningClass { FileName = "a.sql", SqlEyeWarning = "SD009: Missing or order mismatch of Grant statement" });
lstWarningClass.Add(new WarningClass { FileName = "d.sql", SqlEyeWarning = "SD004: Check for existence object then Drop statement before create statement" });



List<RemarkClass> lstRemarkClass = new List<RemarkClass>();
lstRemarkClass.Add(new RemarkClass { FileName = "a.sql", SqlEyeRamark = "SD010: Set AnsiiNullsOn ON statement is missing" });
lstRemarkClass.Add(new RemarkClass { FileName = "b.sql", SqlEyeRamark = "SD002: Order mismatch or it should be ON." });
lstRemarkClass.Add(new RemarkClass { FileName = "c.sql", SqlEyeRamark = "SD002: Order mismatch or it should be ON." });

望ましい出力は

<ScriptFileNames>
    <SqlEye>
        <SqlEyeWarnings Name="Set QuotedIdentifier ON statement is missing">
            <File Name="a.sql" />
            <File Name="b.sql" />
        </SqlEyeWarnings>
        <SqlEyeWarnings Name="SD009: Missing or order mismatch of Grant statement">
            <File Name="a.sql" />
            <File Name="c.sql" />
        </SqlEyeWarnings>
        <SqlEyeWarnings Name="SD004: Check for existence object then Drop statement before create statement">
            <File Name="d.sql" />           
        </SqlEyeWarnings>

        <SqlEyeRemarks Name="SD010: Set AnsiiNullsOn ON statement is missing">
            <File Name="a.sql" />           
        </SqlEyeRemarks>
        <SqlEyeRemarks Name="SD002: Order mismatch or it should be ON.">
            <File Name="b.sql" />   
            <File Name="c.sql" />
        </SqlEyeRemarks>
    </SqlEye>
</ScriptFileNames>
4

2 に答える 2

1
 XmlDocument xml = new XmlDocument();
            XmlElement root = xml.CreateElement("ScriptFileNames");
            xml.AppendChild(root);

            XmlElement comment = xml.CreateElement("SqlEye");
            root.AppendChild(comment);

            var WarningClassData = (from items in lstWarningClass
                                    select items).GroupBy(t => t.SqlEyeWarning).ToList();

            foreach (var data in WarningClassData)
            {
                XmlElement SqlEyeWarnings = xml.CreateElement("SqlEyeWarnings");
                SqlEyeWarnings.SetAttribute("Name", data.Key);
                comment.AppendChild(SqlEyeWarnings);
                for (int i = 0; i < data.Count(); i++)
                {
                    XmlElement File = xml.CreateElement("File");
                    File.SetAttribute("Name", data.ElementAt(i).FileName);
                    SqlEyeWarnings.AppendChild(File);
                }
            }

            var RemarkClassData = (from items in lstRemarkClass
                                   select items).GroupBy(t => t.SqlEyeRamark).ToList();

            foreach (var data in RemarkClassData)
            {
                XmlElement SqlEyeRemarks = xml.CreateElement("SqlEyeRemarks");
                SqlEyeRemarks.SetAttribute("Name", data.Key);
                comment.AppendChild(SqlEyeRemarks);
                for (int i = 0; i < data.Count(); i++)
                {
                    XmlElement File = xml.CreateElement("File");
                    File.SetAttribute("Name", data.ElementAt(i).FileName);
                    SqlEyeRemarks.AppendChild(File);
                }
            }

            xml.Save(@"F:\\test.xml")
于 2013-06-05T12:27:43.393 に答える