XmlTextWriter & XmlTextReader

  • Thread starter Christian Westerlund
  • Start date
C

Christian Westerlund

Hi!

I want to use XmlTextReader & XmlTextWriter at the same time instead of
using XmlDom.
I get an exception when I try to insert elemnts into an existing
xml-file, actually it's when I try to close the writer.

Here is my code, the else is run if there is an file created already, I
have almost the same code in the if except for the switch and I don't
read anything if the file is brand new, the file is well-formed:

else
{
FileStream readFileStream = new FileStream(newXMl, FileMode.Open,
FileAccess.ReadWrite);


reader = new XmlTextReader( readFileStream );
writer = new XmlTextWriter( readFileStream, System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;

reader.Read();
reader.Read();

while( reader.Read() )
{
switch( reader.NodeType )
{

case XmlNodeType.Element:
if( reader.Name == "Car")
{
writer.WriteStartElement("Car");
writer.WriteAttributeString("Model", "FIAT");
writer.WriteAttributeString("Year", "2330");
writer.WriteAttributeString("Items", "2");
writer.WriteAttributeString("Name", "***************");

writer.WriteStartElement("Stuff");
writer.WriteAttributeString("Date", "2004-01-01");


writer.WriteStartElement("Items");
writer.WriteString("10,5;13,5");
writer.WriteEndElement();

writer.WriteEndElement();

writer.WriteEndElement();
}
break;
}
}
writer.Close();


When I run this code, my elements are inserted inside another element
like this, (always at the same position).

<Car Model="1" Year="1999-01-01" Items="2" Name="hmm">
<Stuff Date="2004-03-30 08:34:35">
<Items>10,5;13,5</It<Car Model="FIAT" Year="2330" Items="2"
Name="******************">
<Stuff Date="2004-01-01">
<Items>10,5;13,5</Items>
</Stuff>
</Car>

So basically I want to read from the file until I get to some point
where I want to insert new elements. Isn't it possible?

/Christian
 
N

Nicholas Paldino [.NET/C# MVP]

Christian,

I don't think that the XmlTextWriter and the XmlTextReader were meant to
be used on a file at the same time. I think that if you want to do
something like this, then you should have the text reader point to the
original file, and then have a text writer point to a new file, which you
copy over until you have to make changes. Either that, or use a DOM to read
the contents in, and then modify them as you wish (or an XSTL
transformation).

Either way, I wouldn't try to read and modify the same file at the same
time.

Hope this helps.
 
C

Christian Westerlund

Hi!

I thought it maybe worked that way.
If I work with two files and copy over information from the first file
and at some point insert my data and then continue to copy from the
first file.

How can I do that? Which methods can I use to accomplish something like
this:

while( reader.Read() ){

writer.WriteMethod( theElementJustRead );
if( reader.Name == something )
writer.WriteMethod( insert my data );

}

/Christian
 
M

MFRASER

I want to do a similar thing, because of the overhead required for a DOM
doc, I am running into memory errors. Did you ever get this resolved and if
so did you use a XMLTextReader, XMLTextWriter or a straight TextReader and
TextWriter.
 

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

Similar Threads

xmltextwriter 2
Using XmlTextWriter without closing the base stream 8
Textwriter 1
problem writing xml 3
Leak with XmlTextWriter? 1
Write to XML with C# 5
Submitting XML to an URL 1
XmlTextWriter & MemoryStream 2

Top