2
<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

David
  • 15,894
  • 22
  • 55
  • 66
Georg
  • 117
  • 3
  • 18
  • Are you ought to use XmlTextWriter? LINQ to XML is much simpler to use – Sasha Apr 03 '13 at 12:32
  • 5
    Do you particularly *want* to use XmlTextWriter? Can you use LINQ to XML instead? (It'll make your life much easier...) – Jon Skeet Apr 03 '13 at 12:32

3 Answers3

7

Try using XElement end XAttribute classes. They are part of LINQ to XML and they make work with XML much easier.

var xml = new XElement("Account",
              new XAttribute("nr", 401),
              new XAttribute("name", "Wasser/Abwasser"),
              new XElement("Accounting",
                  new XAttribute("date", "15.02."),
                  new XAttribute("refNr", "...")));

That one returns on .ToString():

<Account nr="401" name="Wasser/Abwasser">
  <Accounting date="15.02." refNr="..." />
</Account>

Fill the rest of attributes following the pattern and you'll have what you want.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
5

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.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
4

Using LINQ to XML you can do it very simply:

var document = new XDocument( 
                   new XElement("Account",
                       new XAttribute("nr", 401),
                          ...));
document.WriteTo(myXmlTextWriter);
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Sasha
  • 8,537
  • 4
  • 49
  • 76