Reputation: 117
<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
Upvotes: 2
Views: 204
Reputation: 67918
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.
Upvotes: 5
Reputation: 8860
Using LINQ to XML you can do it very simply:
var document = new XDocument(
new XElement("Account",
new XAttribute("nr", 401),
...));
document.WriteTo(myXmlTextWriter);
Upvotes: 4
Reputation: 125660
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.
Upvotes: 7