XMLTextReader & Writer

  • Thread starter Christian Westerlund
  • Start date
C

Christian Westerlund

Hi.

I want to use XMLTextWriter & Reader because I heard that they are
faster and don't allocate the entire xml-file in memory like XMLDom do.

However, the reader and writer don´t seem to work well together because
I get a xmlException when I try to read a stream that I just have
written to. Is there any way to read and write simultanously without
using XMLDom?

My other problem is that I want to be able to append data to an existing
xml-file with XMLTextWriter but when I do that the data is saved
after the end of the root element, like this:

<CarRegister version="1">
<Car name="Volvo">134000</Car>
<Car name="FIAT">16400</Car>
</CarRegister>
<Car name="FIAT UNO">13000</Car>

Therefore my xml-file is not well-formed.

Do anyone have any idea, I would really want to use something faster
than XMLDom.

/Christian
 
W

William Ryan eMVP

Christian:

Have you narrowed down what's causing the exception? Check out this link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconWell-FormedXMLCreationWithXmlTextWriter.asp on Creating Well-Formed
XML . I'm guessing that it's on Close where the exception is being thrown
b/c Close ensures that it's valid and well formed . If you posted the code
used to create the XML it would be helpful but this article should help
you...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconWell-FormedXMLCreationWithXmlTextWriter.asp
 
C

Christian Westerlund

Hi!

I don't get an exception longer because I've been re-writing the code.
But basically I want to read the xml-file to a specific element and then
insert an element with the writer.
Is that possible?

/Christian
 
C

Christian Westerlund

Well, I do still get an exception.
I think you're right, 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 have created a xml-file and 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>


/Christian
 
I

Ilya Tumanov [MS]

But basically I want to read the xml-file to a specific element and then
insert an element with the writer.
Is that possible?

XmlTextReader and XmlTextWriter are forward-only, so it's not possible.

You'll have to do it like you would normally do it with a text file (move
the file pointer to the insertion point, read the rest of the file into
memory,
truncate the file at insertion point, append new data, append the rest of
the file from memory).

If DOM is too slow, consider loading XML into custom data storage (e.g.
ArrayList), changing it and saving it back.
If it's not acceptable for some reason, consider using other format you can
easily change, like binary file with fixed size records.

As to XML, it's pretty much immutable; it's really hard to change it once
it has been saved.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

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