XML attributes, second stream?

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
 
N

n!

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

If you require the children in order to compute this value, why is it an
attribute of the parent? XML being hierarchical in nature expects objects to
know their relevant properties, when reading the file back this attribute
would make no sense because your children have not been parsed. I suppose
I'm just confused why your parent has an attribute it is not responsible
for.

Anyway, is there no way for you to simply 'obtain' this value? Attributes
are (in some ways) similar to properties, can you not just have the object
ask itself 'ok whats this value?'. Again, if you can't do that it doesn't
sound like an XML attribute for that object :)

n!
 
P

phoenix

n! said:
write it

If you require the children in order to compute this value, why is it an
attribute of the parent? XML being hierarchical in nature expects objects to
know their relevant properties, when reading the file back this attribute
would make no sense because your children have not been parsed. I suppose
I'm just confused why your parent has an attribute it is not responsible
for.

Anyway, is there no way for you to simply 'obtain' this value? Attributes
are (in some ways) similar to properties, can you not just have the object
ask itself 'ok whats this value?'. Again, if you can't do that it doesn't
sound like an XML attribute for that object :)

n!

I'm having a bitstream which I have to parse and take note about the size of
each of its elements. I now have it simple that each lowest level element
has a size attribute. I can recursively travel to the XML to get the size of
a parent (since it's just the size of all of it's children together and some
leading bits). But since the XML will never be changed i just would find it
useful to know the size of an element including it's children).

Playing around a bit more I have come up with the following code :
========== start code ==========
int number_of_children = 0;
int some_random_number = 5;
FileStream fs = new FileStream(@"c:\test2.txt", FileMode.OpenOrCreate,
FileAccess.Write);
XmlTextWriter xw = new XmlTextWriter(fs, System.Text.Encoding.UTF8);
xw.WriteStartElement("element");
xw.WriteAttributeString("size", "5");
MemoryStream ms = new MemoryStream();
XmlTextWriter xwChild = new XmlTextWriter(ms, System.Text.Encoding.UTF8);
for (int i = 0; i < some_random_number;i++)
{
xwChild.WriteStartElement("childelement");
xwChild.WriteString("child " + i);
xwChild.WriteEndElement();
number_of_children++;
}
xw.WriteAttributeString("number_of_children",
XmlConvert.ToString(number_of_children));
// Merge them back together
xwChild.Flush();
xw.WriteRaw(System.Text.Encoding.UTF8.GetString(ms.ToArray(), 0, (int)
ms.Position));
xw.WriteEndElement();
xw.Close();
========== end code ==========

The following code does what I want now except that between the 'mainstream'
(fs) and the 'childstream' (ms) an unknown symbol appears and I can't find a
reason for it. (well at least in notepad it shows up)

Yves
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top