Search code examples
c#xmltextwriter

How to use XmlTextWriter Element?


I have the following XML:

<Account>
    <nr>6134</nr>
    <name>Summ</name>
    <Accounting>
      <income>200</income>
    </Accounting>
    <summincome>200</summincome>
</Account> 

What i tried and didn't succeed:

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

As you can see i enter a foreach loop calculate the summ and assign it's value to "summincome". Thx in advance for your tipps


Solution

  • They are not attributes they are elements

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