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]