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
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