This topic is 4295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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.

Share on other sites
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.

Share on other sites
Damn, I'm very sorry that I didn't say which language I'd used.
It's indeed C#.

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!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• Forum Statistics

• Total Topics
633682
• Total Posts
3013311
×