C#でxmlファイルを書きたいと思います。そのため、私はXElementオブジェクトを使いたいが、私はできません条件付きでXMLをあまり控え目に書くことはできますか?
<EmployeeConfiguration>
<Bosses>
<Boss name="BOB">
<Employees>
<Employee Id="#0001" />
<Employee Id="#0002" />
</Employees>
<Boss>
</Bosses>
</EmployeeConfiguration>
と私はEmployee
ノードが存在しない場合はEmployees
ノードを持っていたくない...
:それはそのような基本的なファイルです...だから私はXmlWriterを使用しました。それが正常に動作しますが、私はそれがXMLを書くために非常に冗長です見つける:
EmployeeConfiguration config = EmployeeConfiguration.GetConfiguration();
using (XmlWriter writer = XmlWriter.Create(_fileUri, settings))
{
writer.WriteStartDocument();
// <EmployeeConfiguration>
writer.WriteStartElement("EmployeeConfiguration");
if (config.Bosses.Count > 0)
{
// <Bosses>
writer.WriteStartElement("Bosses");
foreach (Boss b in config.Bosses)
{
// Boss
writer.WriteStartElement("Boss");
writer.WriteStartAttribute("name");
writer.WriteString(b.Name);
writer.WriteEndAttribute();
if (b.Employees.Count > 0)
{
writer.WriteStartElement("Employees");
foreach (Employee emp in b.Employees)
{
writer.WriteStartElement(Employee);
writer.WriteStartAttribute(Id);
writer.WriteString(emp.Id);
writer.WriteEndAttribute();
writer.WriteEndElement();
}
}
}
}
}
は、XMLファイルのこの種を書き込むための別の(そして最速の)方法はありますか?
これはかなり効率的です。 –
ええ、実行時には効率的ですが、書きません:( – Florian