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!Filen ame!element], [Caption] AS [Content!1!Capti on!element],
[Content] AS [Content!1!!CDAT A] FROM Content FOR XML EXPLICIT";
SqlConnection myConnection = new SqlConnection(" data
source=WEBSERVE R;database=myDB ;user id=;password=);
try
{
myConnection.Op en();
SqlCommand myCommand = new SqlCommand(myXm lQuery, myConnection);
System.Xml.XmlR eader myXmlReader = myCommand.Execu teXmlReader();
string output="";
output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
output += "<Contents> ";
while (myXmlReader.Re ad())
{
string tmp = "";
// ERROR: It does not generate the closing tag!
if (myXmlReader.Na me != "") tmp += "<"+myXmlReader .Name+">";
if (myXmlReader.Va lue != "") tmp += myXmlReader.Val ue;
output += tmp;
}
output += "</Contents>";
myConnection.Cl ose();
if (myXmlReader != null) myXmlReader.Clo se();
// Save the modified XML document.
FileStream file = File.Create(@"c :\test.xml");
StreamWriter myWriter = new StreamWriter(fi le);
myWriter.Write( output);
myWriter.Close( );
// Close the XmlReader
myXmlReader.Clo se();
}
catch(Exception exc)
{
MessageBox.Show (exc.ToString() +"\n\n"+exc.Get Type());
}
finally
{
myConnection.Cl ose();
} 7 8374
Leszek wrote: 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?
Using XmltexWriter obviously.
XmlWriter w = new XmlTextWriter(" foo.xml", Encoding.UTF8);
reader.MoveToCo ntent();
w.WriteNode(rea der);
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
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.W riteNode() 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!Filen ame!element], "
+
" [Caption] AS [Content!1!Capti on!element], " +
" [Content] AS [Content!1!!CDAT A] " +
"FROM Content " +
"WHERE IsRemoved=0 " +
"FOR XML EXPLICIT;" +
// Create a new connection to a SQL Srv database
SqlConnection myConnection = new SqlConnection(" ...");
try
{
myConnection.Op en();
// Generate XML code. Put the code into the reader.
SqlCommand myCommand = new SqlCommand(myXm lQuery, myConnection);
System.Xml.XmlR eader reader = myCommand.Execu teXmlReader();
// Write XML code to an output file (only the first <Content>elemen t
is written)
System.Xml.XmlT extWriter writer = new
System.Xml.XmlT extWriter(@"c:\ test.xml", Encoding.UTF8);
reader.MoveToCo ntent();
writer.WriteNod e(reader, false);
// Close
reader.Close();
writer.Close();
}
catch(Exception exc)
{
// O kurcze, something went wrong
MessageBox.Show (exc.ToString() +"\n\n"+exc.Get Type());
}
finally
{
myConnection.Cl ose();
}
I would appreciate any help.
Thanks,
Leszek Taratuta
"Oleg Tkachenko" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:O0******** ******@TK2MSFTN GP11.phx.gbl... Leszek wrote:
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?
Using XmltexWriter obviously.
XmlWriter w = new XmlTextWriter(" foo.xml", Encoding.UTF8); reader.MoveToCo ntent(); w.WriteNode(rea der); 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
You could try:
writer.WriteSta rtElement("root ");
writer.WriteRaw (reader.ReadOut erXml, false);
writer.WriteEnd Element();
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" <ta******@5thbu siness.com> wrote in message
news:Oq******** ******@tk2msftn gp13.phx.gbl... 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.W riteNode() 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!Filen ame!element],
" + " [Caption] AS [Content!1!Capti on!element], "
+ " [Content] AS [Content!1!!CDAT A] " + "FROM Content " + "WHERE IsRemoved=0 " + "FOR XML EXPLICIT;" +
// Create a new connection to a SQL Srv database SqlConnection myConnection = new SqlConnection(" ...");
try { myConnection.Op en();
// Generate XML code. Put the code into the reader. SqlCommand myCommand = new SqlCommand(myXm lQuery, myConnection); System.Xml.XmlR eader reader = myCommand.Execu teXmlReader();
// Write XML code to an output file (only the first
<Content>elemen t is written) System.Xml.XmlT extWriter writer = new System.Xml.XmlT extWriter(@"c:\ test.xml", Encoding.UTF8); reader.MoveToCo ntent(); writer.WriteNod e(reader, false);
// Close reader.Close(); writer.Close(); } catch(Exception exc) { // O kurcze, something went wrong MessageBox.Show (exc.ToString() +"\n\n"+exc.Get Type()); } finally { myConnection.Cl ose(); }
I would appreciate any help. Thanks,
Leszek Taratuta
"Oleg Tkachenko" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message news:O0******** ******@TK2MSFTN GP11.phx.gbl... Leszek wrote:
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?
Using XmltexWriter obviously.
XmlWriter w = new XmlTextWriter(" foo.xml", Encoding.UTF8); reader.MoveToCo ntent(); w.WriteNode(rea der); 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
Thanks for your response.
I have tried your method but it does not work. The
writer.WriteRaw (reader.ReadOut erXml()) 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!Filen ame!element], "
+
" [Caption] AS [Content!1!Capti on!element], " +
" [Content] AS [Content!1!!CDAT A] " +
"FROM Content " +
"FOR XML EXPLICIT";
SqlConnection myConnection = new SqlConnection(" ...");
try
{
myConnection.Op en();
SqlCommand myCommand = new SqlCommand(myXm lQuery, myConnection);
System.Xml.XmlR eader reader = myCommand.Execu teXmlReader();
System.Xml.XmlT extWriter writer = new
System.Xml.XmlT extWriter(@"c:\ test.xml", Encoding.UTF8);
writer.WriteSta rtElement("Cont ents");
writer.WriteRaw (reader.ReadOut erXml()); // Does not write anything
:-(
writer.WriteEnd Element();
if (reader != null) reader.Close();
if (writer != null) writer.Close();
}
catch(Exception exc)
{
MessageBox.Show (exc.ToString() +"\n\n"+exc.Get Type());
}
finally
{
myConnection.Cl ose();
}
"Scott Caskey [MSFT]" <sc*****@online .microsoft.com> wrote in message
news:40******** @news.microsoft .com... You could try:
writer.WriteSta rtElement("root "); writer.WriteRaw (reader.ReadOut erXml, false); writer.WriteEnd Element();
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" <ta******@5thbu siness.com> wrote in message news:Oq******** ******@tk2msftn gp13.phx.gbl... 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.W riteNode() 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!Filen ame!element], " + " [Caption] AS [Content!1!Capti on!element],
" + " [Content] AS [Content!1!!CDAT A] " + "FROM Content " + "WHERE IsRemoved=0 " + "FOR XML EXPLICIT;" +
// Create a new connection to a SQL Srv database SqlConnection myConnection = new SqlConnection(" ...");
try { myConnection.Op en();
// Generate XML code. Put the code into the reader. SqlCommand myCommand = new SqlCommand(myXm lQuery, myConnection); System.Xml.XmlR eader reader = myCommand.Execu teXmlReader();
// Write XML code to an output file (only the first <Content>elemen t is written) System.Xml.XmlT extWriter writer = new System.Xml.XmlT extWriter(@"c:\ test.xml", Encoding.UTF8); reader.MoveToCo ntent(); writer.WriteNod e(reader, false);
// Close reader.Close(); writer.Close(); } catch(Exception exc) { // O kurcze, something went wrong MessageBox.Show (exc.ToString() +"\n\n"+exc.Get Type()); } finally { myConnection.Cl ose(); }
I would appreciate any help. Thanks,
Leszek Taratuta
"Oleg Tkachenko" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message news:O0******** ******@TK2MSFTN GP11.phx.gbl... Leszek wrote:
> 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?
Using XmltexWriter obviously.
XmlWriter w = new XmlTextWriter(" foo.xml", Encoding.UTF8); reader.MoveToCo ntent(); w.WriteNode(rea der); 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
Leszek wrote: 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.W riteNode() method exports only the first branch instead of the whole document.
So loop over all top-level elements then:
XmlWriter w = new XmlTextWriter(" foo.xml", Encoding.UTF8);
reader.MoveToCo ntent();
while(reader.Re ad())
w.WriteNode(rea der);
--
Oleg Tkachenko http://www.tkachenko.com/blog
Multiconn Technologies, Israel
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!tkachen ko.com> wrote in message
news:Of******** ******@TK2MSFTN GP10.phx.gbl... Leszek wrote:
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.W riteNode() method
exports only the first branch instead of the whole document.
So loop over all top-level elements then:
XmlWriter w = new XmlTextWriter(" foo.xml", Encoding.UTF8); reader.MoveToCo ntent(); while(reader.Re ad()) w.WriteNode(rea der);
-- Oleg Tkachenko http://www.tkachenko.com/blog Multiconn Technologies, Israel
Leszek wrote: 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 -.
That sounds weird. Show us some sample.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: CGuy |
last post by:
Hi,
I am using an XmlTextReader to read an xml file. It may happen that the
file is present in the disk, but it may be empty (0 bytes). I would like to
find out whether the xml file contains a valid root node or not. How do I do
this?
This is what I need
if(File.Exists(fileName))
{
|
by: xmlguy |
last post by:
I believe this should be pretty elementary, but for some
reason I cannot seem to think of how to write the an XML
file from an incoming XML file.
Basically this is what I do:
Input:
XmlReader instance from another module (beyond my control)
|
by: Matthew Heironimus |
last post by:
According to the XML 1.0 (Third Edition) W3C Recommendation
(http://www.w3.org/TR/2004/REC-xml-20040204/#sec-line-ends) all #xD, #xA,
and #xD#xA character combinations should be converted to a single #xA
character.
According to the "Reading XML with the XmlReader" section of the ".NET
Framework Developer's Guide" on-line help, the XmlReader will not perform
this normalization by default. You can cause the XmlReader to perform this
|
by: Matthew Heironimus |
last post by:
According to the XML 1.0 (Third Edition) W3C Recommendation (http://www.w3.org/TR/2004/REC-xml-20040204/#sec-line-ends) all #xD, #xA, and #xD#xA character combinations should be converted to a single #xA character.
According to the "Reading XML with the XmlReader" section of the ".NET Framework Developer's Guide" on-line help, the XmlReader will not perform this normalization by default. You can cause the XmlReader to perform this...
|
by: Angus Lepper |
last post by:
I'm writing a stock ticker for a stock market simulation, and can load the
data into the xmlreader in the first place, but can't figure out how to
refresh/update the data in it. Any ideas?
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
| |
by: Eckhard Schwabe |
last post by:
I only found one post on Google where someone mentions the same problem
with a DataSet:
XmlDataReader in .Net 1.1 can not read XML files from a path which
contains "%10" or "%3f".
code to reproduce:
string filename = "%10.xml"; //XML file with this name is existing
XmlReader reader = new XmlTextReader(filename);
|
by: Rob Meade |
last post by:
Hi all,
I'm having a few difficulties with the above, ie I cant find any good
examples anywhere of how to do what I want to do!
I have an xml file on my desktop which I want to read in.
Having checked the info for XML file reading it suggests that with .net 2.0
you should use the XMLReader class...
|
by: Franck |
last post by:
Hi,
I'm quit confused with the following problem:
I'm tryin to read an Embedded Xml file from my Class Library using a
static method which should returns a XmlReader.
The point is that using DataSet or XmlDocument works perfectly... using
XmlReader always fails. I always get "None" as a result.
Even using XmlReaderSettings does not change anything.
|
by: heday60 |
last post by:
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...
foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |