문제

<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

도움이 되었습니까?

해결책 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.

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top