Question

<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

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Using LINQ to XML you can do it very simply:

var document = new XDocument( 
                   new XElement("Account",
                       new XAttribute("nr", 401),
                          ...));
document.WriteTo(myXmlTextWriter);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top