我有两个班级
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>
答案 0 :(得分: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")
答案 1 :(得分:0)
这是您可以使用的通用数据结构:
class SqlEyeData
{
public string Name {get;set;}
public List<string> Filenames {get;set;}
}
这是您可以用来根据以前的代码填充这些结构的代码(使用GroupBy的Bunny&Linq查询更加优雅):
var sqlEyeWarnings = new List<SqlEyeData>();
var uniqueWarningMessages = lstWarningClass.Select(warning => warning.SqlEyeWarning).Distinct().ToList().ForEach(msg => {
sqlEyeWarnings.Add(new SqlEyeData()
{
Name = msg,
Filenames = lstWarningClass.Where(warning => warning.SqlEyeWarning == msg).Select(warning => warning.FileName).ToList(),
});
});
我刚刚完成了WarningClass实现,但RemarkClass与不同的属性名称完全相同。实际上,通过使用WarningClass和RemarkClass的单个类可以省去很多麻烦,因为它们之间的唯一区别是字符串属性的名称。
然后你可以做这样的事情来转换使用XDocument:
public XElement GetSqlEyeDataElement(string elementName, SqlEyeData data)
{
var returnValue = new XElement(elementName,
new XAttribute("Name", data.Name)
);
data.Filenames.ForEach(filename =>
{
returnValue.Add(new XElement("File",
new XAttribute("Name", filename)));
});
return returnValue;
}