我有以下XML:
<Account>
<nr>6134</nr>
<name>Summ</name>
<Accounting>
<income>200</income>
</Accounting>
<summincome>200</summincome>
</Account>
我尝试过但没有成功:
myXmlTextWriter2.WriteStartElement("Account");
myXmlTextWriter2.WriteAttributeString("nr", AccountNumber.ToString());
myXmlTextWriter2.WriteAttributeString("name", Name);
double summ=0;
foreach (AccountRecord ar in kp)
{
myXmlTextWriter2.WriteStartElement("Accounting");
myXmlTextWriter2.WriteAttributeString("income", ar.Amount.ToString());
myXmlTextWriter2.WriteEndElement();
summ += ar.Amount;
}
myXmlTextWriter2.WriteAttributeString("summincome", summ.ToString());
myXmlTextWriter2.WriteEndElement();
如您所见,我输入一个foreach循环计算summ并将其值分配给“summincome”。提前为你的tipps
答案 0 :(得分:1)
试试这个
using (XmlWriter writer = XmlWriter.Create("accounts.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Account");
writer.WriteElementString("nr", AccountNumber.ToString());
writer.WriteElementString("name", Name);
double summ=0;
foreach (AccountRecord ar in kp)
{
writer.WriteStartElement("Accounting");
writer.WriteElementString("income", ar.Amount.ToString());
writer.WriteEndElement();
summ += ar.Amount;
}
writer.WriteElementString("summincome", summ.ToString())
writer.WriteEndElement();
writer.WriteEndDocument();
}
答案 1 :(得分:1)
它们不是属性的属性
myXmlTextWriter2.WriteStartElement("Account");
myXmlTextWriter2.WriteAttributeString("nr", AccountNumber.ToString());
myXmlTextWriter2.WriteAttributeString("name", Name);
double summ=0;
foreach (AccountRecord ar in kp)
{
myXmlTextWriter2.WriteStartElement("Accounting");
myXmlTextWriter2.WriteElementString("income", ar.Amount.ToString());
myXmlTextWriter2.WriteEndElement();
summ += ar.Amount;
}
myXmlTextWriter2.WriteElementString("summincome", summ.ToString());
myXmlTextWriter2.WriteEndElement();