adding to a xml-file

Started by
2 comments, last by da_cobra 17 years, 1 month ago
I want this :
[source
<?xml version="1.0" encoding="UTF-8"?>
<Highscore>
<Line>
<Name>Da_cobra</Name>
<Score>9500</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>9000</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>8500</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>8000</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>7500</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>7000</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>6500</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>6000</Score>
</Line>
<Line>
<Name>Da_cobra</Name>
<Score>5500</Score>
</Line>
</Highscore>


but I have this :

<?xml version="1.0" encoding="UTF-8"?>
<Highscore>
<Line>
<Name>Da_cobraDa_cobraDa_cobraDa_cobraDa_cobraDa_cobraDa_cobraDa_cobraDa_cobra</Name>
<Score>950090008500800075007000650060005500</Score>
</Line>
</Highscore>


with this code :

XmlNode root = xmlDoc.DocumentElement;
XmlText textNode ;
XmlElement lineNode = xmlDoc.CreateElement("Line");
XmlElement nameNode = xmlDoc.CreateElement("Name");
XmlElement scoreNode = xmlDoc.CreateElement("Score");

for (int iCounter = 0; iCounter < 10; iCounter++)
{
textNode = xmlDoc.CreateTextNode(strDefaultName);

root.AppendChild(lineNode);

lineNode.AppendChild(nameNode);
nameNode.AppendChild(textNode);

textNode = xmlDoc.CreateTextNode((lDefaultScore - (iCounter * lLowerScore)).ToString());

lineNode.AppendChild(scoreNode);
scoreNode.AppendChild(textNode);
}


As you can see I should have 10 child nodes "lines" with 2 child nodes in it "name" and "score" I only have 9 "lines" nodes (least of my problems for now), but somehow with my code all the names are put behind each other and also all the scores?!? what am I doing wrong?
Advertisement
I'm not sure which language you're using, but my guess is you need to create new XmlElement for every single element. i.e. the XmlElement *Node = new XmlElement ... should be *inside* the loop, not outside of it.
That looks like the .Net XML stuff, yes? So C# I'm guessing.

You need to create and append nodes for each element.

If this is a batch write, I find the XmlWriter a lot simpler and cleaner to use. The inside of your loop would look something like this:

wtr.WriteStartElement("Line");wtr.WriteElementString("Name", strDefaultName);wtr.WriteElementString("Score",     (lDefaultScore - (iCounter * lLowerScore)).ToString());wtr.WriteEndElement();


If you use an XmlWriter, make sure to create an XmlWriterOptions (I think its called) for its constructor and set the Indent property to true. The default is to generate a text stream without any newlines. Indent=true on the XmlWriterOptions will format the stream like you probably want it.
Damn, I'm very sorry that I didn't say which language I'd used.
It's indeed C#.

I'll try your advise asap.


thx for replying!


edit : Both solutions worked for me, but I used dalep's solution, because it's shorter and easier, but still thx alot for helping me out, both!

This topic is closed to new replies.

Advertisement