Reputation: 3504
Hey i am unable to figure out why this xml is not being generated properly.
Here is my xml being generated:
<?xml version="1.0"?>
<schedules>
<schedule>
<Name>Schedule0</Name>
<Task 0>
<description>task0 description</description>
<done>true</done>
</Task 0>
<Task 1>
<description>task1 description</description>
<done>true</done>
</Task 1>
</schedule>
<schedule>
<Name>Schedule1</Name>
<Task 0>
<description>task0 description</description>
<done>true</done>
</Task 0>
<Task 1>
<description>task1 description</description>
<done>true</done>
</Task 1>
</schedule>
<schedule>
<Name>Schedule2</Name>
<Task 0>
<description>task0 description</description>
<done>true</done>
</Task 0>
<Task 1>
<description>task1 description</description>
<done>true</done>
</Task 1>
</schedule>
<schedule>
<Name>Schedule3</Name>
<Task 0>
<description>task0 description</description>
<done>true</done>
</Task 0>
<Task 1>
<description>task1 description</description>
<done>true</done>
</Task 1>
</schedule>
<schedule>
<Name>Schedule4</Name>
<Task 0>
<description>task0 description
</description>
<done>true</done>
</Task 0>
<Task 1>
<description>task1 description</description>
<done>true</done>
</Task 1>
</schedu
And my c# code :
XmlTextWriter xmlwriter = new XmlTextWriter("mojixml",null);
xmlwriter.WriteStartDocument();
xmlwriter.WriteStartElement("schedules");
for (int i = 0; i < 5; i++)
{
xmlwriter.WriteStartElement("schedule");
xmlwriter.WriteElementString("Name", "Schedule"+i.ToString());
for (int ii = 0; ii < 2; ii++)
{
xmlwriter.WriteStartElement("Task " + ii.ToString());
xmlwriter.WriteElementString("description", "task"+ii+ " description");
xmlwriter.WriteElementString("done", "true");
xmlwriter.WriteEndElement();
}
xmlwriter.WriteEndElement(); //schedule
}
xmlwriter.WriteEndElement(); //schedules
xmlwriter.WriteEndDocument();
also when i change the outer loop range from 5 to 3 it doesn't show any xml(empty file).
Upvotes: 0
Views: 155
Reputation: 1499770
You're never closing or flushing your writer - you're getting to the end of the program, at which point you've got a writer with the XML in its buffer, waiting to be written... but your program just quits. Just putting it in a using
statement will do the right thing, disposing of the stream appropriately, even if there's an exception:
using (XmlTextWriter xmlwriter = new XmlTextWriter("mojixml", null))
{
}
However, it would be better still to use XmlWriter.Create
:
using (XmlWriter xmlwriter = XmlWriter.Create("schedules.xml"))
{
}
At this point you'll also need to fix your generation code not to create an element name with a space in it:
xmlwriter.WriteStartElement("Task " + ii.ToString());
// becomes...
xmlwriter.WriteStartElement("Task" + ii.ToString());
Also note that all those calls to ToString
are unnecessary - you can leave the compiler to do that.
Finally, I'd suggest that unless you're building a huge file, it would be simpler to use LINQ to XML to build a document in memory and then write it out at the end.
Upvotes: 1