Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear all,
here is my code.
i can able to create the xml file.
but it is writing only last row value .
not writing all the rows.


public void drtoXML()
{
SqlConnection Conn = new SqlConnection("Data Source=;Initial Catalog=Employee;");
string sqlText = "select * from EMP FOR XML AUTO";
SqlCommand dataCmd = new SqlCommand(sqlText, Conn);
XmlWriter writer = null;
Conn.Open();
XmlReader dataReader = dataCmd.ExecuteXmlReader();
while (dataReader.Read())
{
using (writer = XmlWriter.Create(@"D:\Manas\Prod.xml"))
{
writer.WriteNode(dataReader, true);

}

}
dataReader.Close();
Conn.Close();

}



please help me.
Posted

1 solution

You were creating new xml file inside loop, which was deleting old xml file
Below is updated code.


public void drtoXML()
{
SqlConnection Conn = new SqlConnection("Data Source=;Initial Catalog=Employee;");
string sqlText = "select * from EMP FOR XML AUTO";
SqlCommand dataCmd = new SqlCommand(sqlText, Conn);
XmlWriter writer = null;
Conn.Open();
XmlReader dataReader = dataCmd.ExecuteXmlReader();
using (writer = XmlWriter.Create(@"D:\Manas\Prod.xml"))
{

while (dataReader.Read())
{
writer.WriteNode(dataReader, true);
}

}
dataReader.Close();
Conn.Close();
 
}
 
Share this answer
 
Comments
connect2manas 28-Jun-13 2:40am    
thanks for reply.
but after editing the error is on writer.WriteNode(dataReader, true);
Error is:

Token StartElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.
Mahesh Bailwal 28-Jun-13 3:14am    
The problem here is that you always have to put the root element into your document. Please refer below links

http://stackoverflow.com/questions/209554/token-text-in-state-endrootelement-would-result-in-an-invalid-xml-document

http://www.dotnetperls.com/xmlwriter
connect2manas 28-Jun-13 3:12am    
Hi Mahesh,
i have added the following code for the above error.
now no error.
but the result is coming alternet row.
like first row,3rd row,5th row like this.

plz help me.


string sqlText = "select EID,ENAME from EMP FOR XML AUTO";
SqlCommand dataCmd = new SqlCommand(sqlText, Conn);
XmlWriter writer = null;
Conn.Open();
XmlReader dataReader = dataCmd.ExecuteXmlReader();
XmlWriterSettings settings = new XmlWriterSettings()
{
ConformanceLevel = ConformanceLevel.Auto
};
using (writer = XmlWriter.Create(@"D:\Manas\Prodr.xml",settings))
{

while (dataReader.Read())
{
writer.WriteNode(dataReader, true);
}

}
dataReader.Close();
Conn.Close();
Mahesh Bailwal 28-Jun-13 3:20am    
Try using ConformanceLevel.Fragment
connect2manas 28-Jun-13 3:32am    
hi mahesh,
it is not working .same result like alternet row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900