XmlWriter.WriteNode() does not write tags for any Elements

The Mask

New Member
I am working on a C#.NET program that is attempting to write the XML datareturned from SQL Server, that is to be accessed by an XmlReader returned by the \[code\]SqlCommand.ExecuteXmlReader()\[/code\] method. The SqlCommand query text is "\[code\]SELECT ... FOR XML, ELEMENTS\[/code\]".The code copies the XML data from an XmlReader to an XmlWriter, by using the XmlWriter.WriteNode() method.BUT XmlWriter.WriteNode() does not write the start tag and end tag for any Elements;It writes only the Text content for the Elements.Obviously my intention is to write the tags as well as the Text for the Elements.I have included the code and the output from the SELECT Query and WriteNode() method.below.Does anybody have an explanation why XmlWriter.WriteNode() does not write the tagsand writes only the Text data? C#:\[code\]protected void Page_Load(object sender, EventArgs e){ XmlDocument xmlDocument = new XmlDocument(); XPathNavigator xPathNavigator = xmlDocument.CreateNavigator(); string connectionStr = "Data Source=.\\SQLEXPRESS;" + "Initial Catalog=AdventureWorks;" + "Integrated Security=True"; using (SqlConnection sqlConnection = new SqlConnection(connectionStr)) { SqlCommand sqlCommand = new SqlCommand("SELECT TOP 1 ContactID, " + " Title, " + " FirstName, MiddleName, LastName, " + " Suffix, " + " EmailAddress, Phone " + " FROM Person.Contact AS Contact " + " FOR XML AUTO, ELEMENTS ", sqlConnection); sqlConnection.Open(); using (XmlWriter xmlWriter = xPathNavigator.PrependChild()) { xmlWriter.WriteStartElement("Contacts"); using (XmlReader xmlReader = sqlCommand.ExecuteXmlReader()) { xmlWriter.WriteNode(xmlReader, true); } xmlWriter.WriteEndElement(); } } Response.ContentType = "text/xml"; xmlDocument.Save(Response.Output);} \[/code\]Result set returned in SQL Server Management Studio: \[code\]<Contact> <ContactID>1</ContactID> <Title>Mr.</Title> <FirstName>Gustavo</FirstName> <LastName>Achong</LastName> <EmailAddress>[email protected]</EmailAddress> <Phone>398-555-0132</Phone></Contact>\[/code\].aspx output:1 Mr. Gustavo Achong [email protected] 398-555-0132
 
Back
Top