Search code examples
c#xmlxmltextwriter

How to Write XML in c#?


<Account nr="401" name="Wasser/Abwasser" income="0.00" expenditure="1,310.74" saldo="-1,310.74" resultText="Es wurden ausgegeben">
    <Accounting date="15.02." refNr="....." description="I/2013"  income="" expenditure="1,310.74" vat="10%" pretax="131.07"/>
  </Account>

I can use XmlTextWriter but dont know how to continue with nr,name.....

myXmlTextWriter.WriteStartElement("Account");.....
myXmlTextWriter.WriteElementString("Accounting",......

thx


Solution

  • You'll want to issue a WriteAttributeString:

    myXmlTextWriter.WriteAttributeString(null, "nr", null, "401");
    myXmlTextWriter.WriteEndElement();
    

    and do that right after the WriteStartElement.

    You could probably use this overload too:

    myXmlTextWriter.WriteAttributeString("nr", "401");
    

    and of course replicate that for all other attributes. And it would work the same for the child node as well.