P
phoenix
Hello,
I have the following code :
============ start of code ============
int number_of_children = 0;
int some_random_number = 5;
FileStream fs = new FileStream(@"c:\test.txt", FileMode.OpenOrCreate,
FileAccess.Write);
XmlTextWriter xw = new XmlTextWriter(fs, System.Text.Encoding.UTF8);
xw.WriteStartElement("element");
xw.WriteAttributeString("some_attribute", "5");
for (int i = 0; i < some_random_number)
{
xw.WriteStartElement("childelement");
xw.WriteString("the first child");
xw.WriteEndElement();
number_of_children++;
}
// This will crash since an XmlTextWriter will only go forward.
xw.WriteAttributeString("number_of_children",
XmlConvert.ToString(number_of_children));
xw.WriteEndElement();
============ end of code ============
This is a simplified example of my problem. My problem is I need to write an
attribute of which the value is unknown beforehand. I need to do a whole
bunch of calculations using it's children to get the exact value. I could
offcourse let the routine run twice, once to get the value, then write it
into an attribute, and then run all the 'calculations' again adding the
child elements. But it looks like this is gonna take to much time.
Therefor I'm looking for a way to create a second stream (MemoryStream or
so?) to write all the children in. Then when I wrote the calculated
attribute I would want to write the MemoryStream (or any alternative) after
it. I can write the children to the MemoryStream but I don't know how to
append a MemoryStream to an XmlTextWriter. Any ideas? Or alternatives on how
to solve the problem?
TIA
Yves
I have the following code :
============ start of code ============
int number_of_children = 0;
int some_random_number = 5;
FileStream fs = new FileStream(@"c:\test.txt", FileMode.OpenOrCreate,
FileAccess.Write);
XmlTextWriter xw = new XmlTextWriter(fs, System.Text.Encoding.UTF8);
xw.WriteStartElement("element");
xw.WriteAttributeString("some_attribute", "5");
for (int i = 0; i < some_random_number)
{
xw.WriteStartElement("childelement");
xw.WriteString("the first child");
xw.WriteEndElement();
number_of_children++;
}
// This will crash since an XmlTextWriter will only go forward.
xw.WriteAttributeString("number_of_children",
XmlConvert.ToString(number_of_children));
xw.WriteEndElement();
============ end of code ============
This is a simplified example of my problem. My problem is I need to write an
attribute of which the value is unknown beforehand. I need to do a whole
bunch of calculations using it's children to get the exact value. I could
offcourse let the routine run twice, once to get the value, then write it
into an attribute, and then run all the 'calculations' again adding the
child elements. But it looks like this is gonna take to much time.
Therefor I'm looking for a way to create a second stream (MemoryStream or
so?) to write all the children in. Then when I wrote the calculated
attribute I would want to write the MemoryStream (or any alternative) after
it. I can write the children to the MemoryStream but I don't know how to
append a MemoryStream to an XmlTextWriter. Any ideas? Or alternatives on how
to solve the problem?
TIA
Yves