Connecting Tech Pros Worldwide Forums | Help | Site Map

Exporting from XMLReader to an XML file

Leszek
Guest
 
Posts: n/a
#1: Nov 12 '05
Hello,

I have an XmlReader object containg some Xml code. I would like to export
this document from the reader to an Xml file.

How to do this?

Thanks for any hints,
Leszek Taratuta

Below there is the code I'm using:
string myXmlQuery = "SELECT 1 AS Tag, NULL AS Parent, [Filename] AS
[Content!1!Filename!element], [Caption] AS [Content!1!Caption!element],
[Content] AS [Content!1!!CDATA] FROM Content FOR XML EXPLICIT";

SqlConnection myConnection = new SqlConnection("data
source=WEBSERVER;database=myDB;user id=;password=);

try

{

myConnection.Open();

SqlCommand myCommand = new SqlCommand(myXmlQuery, myConnection);

System.Xml.XmlReader myXmlReader = myCommand.ExecuteXmlReader();

string output="";

output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

output += "<Contents>";

while (myXmlReader.Read())

{

string tmp = "";

// ERROR: It does not generate the closing tag!

if (myXmlReader.Name != "") tmp += "<"+myXmlReader.Name+">";

if (myXmlReader.Value != "") tmp += myXmlReader.Value;

output += tmp;

}

output += "</Contents>";

myConnection.Close();

if (myXmlReader != null) myXmlReader.Close();

// Save the modified XML document.

FileStream file = File.Create(@"c:\test.xml");

StreamWriter myWriter = new StreamWriter(file);

myWriter.Write(output);

myWriter.Close();

// Close the XmlReader

myXmlReader.Close();

}

catch(Exception exc)

{

MessageBox.Show(exc.ToString()+"\n\n"+exc.GetType( ));

}

finally

{

myConnection.Close();

}





Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Exporting from XMLReader to an XML file


Leszek wrote:
[color=blue]
> I have an XmlReader object containg some Xml code. I would like to export
> this document from the reader to an Xml file.
>
> How to do this?[/color]

Using XmltexWriter obviously.

XmlWriter w = new XmlTextWriter("foo.xml", Encoding.UTF8);
reader.MoveToContent();
w.WriteNode(reader);
w.Close();
[color=blue]
> output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";[/color]

Oh, please, don't use this technique. It's year 2003 today, not 1999.
Always use XML classes (e.g. XmlTextWriter) to produce XML.
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Leszek
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Exporting from XMLReader to an XML file


Thanks. It works fine, although there is another problem related to
obtaining well-formed XML code from the SQL database. I can't generate a
root wrapper for the code, so the XmlTextWriter.WriteNode() method exports
only the first branch instead of the whole document.
Below there is code I am using:

// The following query generates a bunch of <Content> elements witout a root
wrapper!
string myXmlQuery = "SELECT 1 AS Tag, " +
" NULL AS Parent, " +
" [Filename] AS [Content!1!Filename!element], "
+
" [Caption] AS [Content!1!Caption!element], " +
" [Content] AS [Content!1!!CDATA] " +
"FROM Content " +
"WHERE IsRemoved=0 " +
"FOR XML EXPLICIT;" +

// Create a new connection to a SQL Srv database
SqlConnection myConnection = new SqlConnection("...");

try
{
myConnection.Open();

// Generate XML code. Put the code into the reader.
SqlCommand myCommand = new SqlCommand(myXmlQuery, myConnection);
System.Xml.XmlReader reader = myCommand.ExecuteXmlReader();

// Write XML code to an output file (only the first <Content>element
is written)
System.Xml.XmlTextWriter writer = new
System.Xml.XmlTextWriter(@"c:\test.xml", Encoding.UTF8);
reader.MoveToContent();
writer.WriteNode(reader, false);

// Close
reader.Close();
writer.Close();
}
catch(Exception exc)
{
// O kurcze, something went wrong
MessageBox.Show(exc.ToString()+"\n\n"+exc.GetType( ));
}
finally
{
myConnection.Close();
}

I would appreciate any help.
Thanks,

Leszek Taratuta

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O0f9GW42DHA.2000@TK2MSFTNGP11.phx.gbl...[color=blue]
> Leszek wrote:
>[color=green]
> > I have an XmlReader object containg some Xml code. I would like to[/color][/color]
export[color=blue][color=green]
> > this document from the reader to an Xml file.
> >
> > How to do this?[/color]
>
> Using XmltexWriter obviously.
>
> XmlWriter w = new XmlTextWriter("foo.xml", Encoding.UTF8);
> reader.MoveToContent();
> w.WriteNode(reader);
> w.Close();
>[color=green]
> > output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";[/color]
>
> Oh, please, don't use this technique. It's year 2003 today, not 1999.
> Always use XML classes (e.g. XmlTextWriter) to produce XML.
> --
> Oleg Tkachenko
> XmlInsider
> http://blog.tkachenko.com[/color]


Scott Caskey [MSFT]
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Exporting from XMLReader to an XML file


You could try:

writer.WriteStartElement("root");
writer.WriteRaw(reader.ReadOuterXml, false);
writer.WriteEndElement();

This should wrap your xml inside a root node.

Scott

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Leszek" <taratuta@5thbusiness.com> wrote in message
news:OqGlj9D3DHA.1752@tk2msftngp13.phx.gbl...[color=blue]
> Thanks. It works fine, although there is another problem related to
> obtaining well-formed XML code from the SQL database. I can't generate a
> root wrapper for the code, so the XmlTextWriter.WriteNode() method exports
> only the first branch instead of the whole document.
> Below there is code I am using:
>
> // The following query generates a bunch of <Content> elements witout a[/color]
root[color=blue]
> wrapper!
> string myXmlQuery = "SELECT 1 AS Tag, " +
> " NULL AS Parent, " +
> " [Filename] AS [Content!1!Filename!element],[/color]
"[color=blue]
> +
> " [Caption] AS [Content!1!Caption!element], "[/color]
+[color=blue]
> " [Content] AS [Content!1!!CDATA] " +
> "FROM Content " +
> "WHERE IsRemoved=0 " +
> "FOR XML EXPLICIT;" +
>
> // Create a new connection to a SQL Srv database
> SqlConnection myConnection = new SqlConnection("...");
>
> try
> {
> myConnection.Open();
>
> // Generate XML code. Put the code into the reader.
> SqlCommand myCommand = new SqlCommand(myXmlQuery, myConnection);
> System.Xml.XmlReader reader = myCommand.ExecuteXmlReader();
>
> // Write XML code to an output file (only the first[/color]
<Content>element[color=blue]
> is written)
> System.Xml.XmlTextWriter writer = new
> System.Xml.XmlTextWriter(@"c:\test.xml", Encoding.UTF8);
> reader.MoveToContent();
> writer.WriteNode(reader, false);
>
> // Close
> reader.Close();
> writer.Close();
> }
> catch(Exception exc)
> {
> // O kurcze, something went wrong
> MessageBox.Show(exc.ToString()+"\n\n"+exc.GetType( ));
> }
> finally
> {
> myConnection.Close();
> }
>
> I would appreciate any help.
> Thanks,
>
> Leszek Taratuta
>
> "Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> news:O0f9GW42DHA.2000@TK2MSFTNGP11.phx.gbl...[color=green]
> > Leszek wrote:
> >[color=darkred]
> > > I have an XmlReader object containg some Xml code. I would like to[/color][/color]
> export[color=green][color=darkred]
> > > this document from the reader to an Xml file.
> > >
> > > How to do this?[/color]
> >
> > Using XmltexWriter obviously.
> >
> > XmlWriter w = new XmlTextWriter("foo.xml", Encoding.UTF8);
> > reader.MoveToContent();
> > w.WriteNode(reader);
> > w.Close();
> >[color=darkred]
> > > output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";[/color]
> >
> > Oh, please, don't use this technique. It's year 2003 today, not 1999.
> > Always use XML classes (e.g. XmlTextWriter) to produce XML.
> > --
> > Oleg Tkachenko
> > XmlInsider
> > http://blog.tkachenko.com[/color]
>
>[/color]


Leszek
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Exporting from XMLReader to an XML file


Thanks for your response.

I have tried your method but it does not work. The
writer.WriteRaw(reader.ReadOuterXml()) method does not export anything. It
seems that this is still the same problem: the XML code held by the reader
is not well-formed (there is no a root wrapper).

Does anybody have an idea how to add the root wrapper to the XML code?

Thanks,
Leszek

Below there is complete code:

// Maybe a solution would be to generate a well-formed XML document at
first place, I mean directly from the SQL Server database instead of dealing
with XML code without a root wrapper?
string myXmlQuery = "SELECT 1 AS Tag, " +
" NULL AS Parent, " +
" [Filename] AS [Content!1!Filename!element], "
+
" [Caption] AS [Content!1!Caption!element], " +
" [Content] AS [Content!1!!CDATA] " +
"FROM Content " +
"FOR XML EXPLICIT";

SqlConnection myConnection = new SqlConnection("...");

try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(myXmlQuery, myConnection);

System.Xml.XmlReader reader = myCommand.ExecuteXmlReader();
System.Xml.XmlTextWriter writer = new
System.Xml.XmlTextWriter(@"c:\test.xml", Encoding.UTF8);

writer.WriteStartElement("Contents");
writer.WriteRaw(reader.ReadOuterXml()); // Does not write anything
:-(
writer.WriteEndElement();

if (reader != null) reader.Close();
if (writer != null) writer.Close();
}
catch(Exception exc)
{
MessageBox.Show(exc.ToString()+"\n\n"+exc.GetType( ));
}
finally
{
myConnection.Close();
}

"Scott Caskey [MSFT]" <scaskey@online.microsoft.com> wrote in message
news:40082f00$1@news.microsoft.com...[color=blue]
> You could try:
>
> writer.WriteStartElement("root");
> writer.WriteRaw(reader.ReadOuterXml, false);
> writer.WriteEndElement();
>
> This should wrap your xml inside a root node.
>
> Scott
>
> --
> This posting is provided "AS IS" with no warranties, and confers no[/color]
rights.[color=blue]
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm
> "Leszek" <taratuta@5thbusiness.com> wrote in message
> news:OqGlj9D3DHA.1752@tk2msftngp13.phx.gbl...[color=green]
> > Thanks. It works fine, although there is another problem related to
> > obtaining well-formed XML code from the SQL database. I can't generate a
> > root wrapper for the code, so the XmlTextWriter.WriteNode() method[/color][/color]
exports[color=blue][color=green]
> > only the first branch instead of the whole document.
> > Below there is code I am using:
> >
> > // The following query generates a bunch of <Content> elements witout a[/color]
> root[color=green]
> > wrapper!
> > string myXmlQuery = "SELECT 1 AS Tag, " +
> > " NULL AS Parent, " +
> > " [Filename] AS[/color][/color]
[Content!1!Filename!element],[color=blue]
> "[color=green]
> > +
> > " [Caption] AS [Content!1!Caption!element],[/color][/color]
"[color=blue]
> +[color=green]
> > " [Content] AS [Content!1!!CDATA] " +
> > "FROM Content " +
> > "WHERE IsRemoved=0 " +
> > "FOR XML EXPLICIT;" +
> >
> > // Create a new connection to a SQL Srv database
> > SqlConnection myConnection = new SqlConnection("...");
> >
> > try
> > {
> > myConnection.Open();
> >
> > // Generate XML code. Put the code into the reader.
> > SqlCommand myCommand = new SqlCommand(myXmlQuery, myConnection);
> > System.Xml.XmlReader reader = myCommand.ExecuteXmlReader();
> >
> > // Write XML code to an output file (only the first[/color]
> <Content>element[color=green]
> > is written)
> > System.Xml.XmlTextWriter writer = new
> > System.Xml.XmlTextWriter(@"c:\test.xml", Encoding.UTF8);
> > reader.MoveToContent();
> > writer.WriteNode(reader, false);
> >
> > // Close
> > reader.Close();
> > writer.Close();
> > }
> > catch(Exception exc)
> > {
> > // O kurcze, something went wrong
> > MessageBox.Show(exc.ToString()+"\n\n"+exc.GetType( ));
> > }
> > finally
> > {
> > myConnection.Close();
> > }
> >
> > I would appreciate any help.
> > Thanks,
> >
> > Leszek Taratuta
> >
> > "Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> > news:O0f9GW42DHA.2000@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Leszek wrote:
> > >
> > > > I have an XmlReader object containg some Xml code. I would like to[/color]
> > export[color=darkred]
> > > > this document from the reader to an Xml file.
> > > >
> > > > How to do this?
> > >
> > > Using XmltexWriter obviously.
> > >
> > > XmlWriter w = new XmlTextWriter("foo.xml", Encoding.UTF8);
> > > reader.MoveToContent();
> > > w.WriteNode(reader);
> > > w.Close();
> > >
> > > > output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
> > >
> > > Oh, please, don't use this technique. It's year 2003 today, not 1999.
> > > Always use XML classes (e.g. XmlTextWriter) to produce XML.
> > > --
> > > Oleg Tkachenko
> > > XmlInsider
> > > http://blog.tkachenko.com[/color]
> >
> >[/color]
>
>[/color]


Oleg Tkachenko
Guest
 
Posts: n/a
#6: Nov 12 '05

re: Exporting from XMLReader to an XML file


Leszek wrote:
[color=blue]
> Thanks. It works fine, although there is another problem related to
> obtaining well-formed XML code from the SQL database. I can't generate a
> root wrapper for the code, so the XmlTextWriter.WriteNode() method exports
> only the first branch instead of the whole document.[/color]

So loop over all top-level elements then:

XmlWriter w = new XmlTextWriter("foo.xml", Encoding.UTF8);
reader.MoveToContent();
while(reader.Read())
w.WriteNode(reader);

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Leszek
Guest
 
Posts: n/a
#7: Nov 12 '05

re: Exporting from XMLReader to an XML file


Thanks.
I still have some problems with well-formed XML. I generated a root wrapper
straight from SQL Server but now the XmlReader does not accept two
characters although they are within CDATA block: 'and -.

I do not understand it at all so in the meantime I exported the XML code
from SQL Server using an ugly solution: traversing all records and creating
XML tags in-fly then saving them to a text file.

Leszek

"Oleg Tkachenko" <oleg@no_!spam!_please!tkachenko.com> wrote in message
news:Of7#5rO3DHA.2144@TK2MSFTNGP10.phx.gbl...[color=blue]
> Leszek wrote:
>[color=green]
> > Thanks. It works fine, although there is another problem related to
> > obtaining well-formed XML code from the SQL database. I can't generate a
> > root wrapper for the code, so the XmlTextWriter.WriteNode() method[/color][/color]
exports[color=blue][color=green]
> > only the first branch instead of the whole document.[/color]
>
> So loop over all top-level elements then:
>
> XmlWriter w = new XmlTextWriter("foo.xml", Encoding.UTF8);
> reader.MoveToContent();
> while(reader.Read())
> w.WriteNode(reader);
>
> --
> Oleg Tkachenko
> http://www.tkachenko.com/blog
> Multiconn Technologies, Israel[/color]


Oleg Tkachenko
Guest
 
Posts: n/a
#8: Nov 12 '05

re: Exporting from XMLReader to an XML file


Leszek wrote:
[color=blue]
> I still have some problems with well-formed XML. I generated a root wrapper
> straight from SQL Server but now the XmlReader does not accept two
> characters although they are within CDATA block: 'and -.[/color]
That sounds weird. Show us some sample.
[color=blue]
> I do not understand it at all so in the meantime I exported the XML code
> from SQL Server using an ugly solution: traversing all records and creating
> XML tags in-fly then saving them to a text file.[/color]
Really bad one :( But you still can use XmlWriter to build XML,
otherwise prepare to learn complete details of hardcore XML syntax.
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Closed Thread


Similar .NET Framework bytes