I am trying to using C#Net2008 to create XML File.
--------------------------------------------------
The User are prompted by folderBrowserDi alog control to select the folder to store the XML File. But the variable which contained the folder path generated this error message:
Coding that generate the error:
Error Message:
The Given path's format is not supported.
----------------------------------------------------
Another problem is a XML File format is not right:
Here is the format display by using NOTEPAD:
-------------------------------------------------------------------------
Here are the full coding that generate XML File with hardcoding FolderPath instead of using User Prompted option.
--------------------------------------------------
The User are prompted by folderBrowserDi alog control to select the folder to store the XML File. But the variable which contained the folder path generated this error message:
Coding that generate the error:
Code:
XmlTextWriter XmlWriter = new XmlTextWriter(strPathName,System.Text.Encoding.UTF8 );
The Given path's format is not supported.
----------------------------------------------------
Another problem is a XML File format is not right:
Here is the format display by using NOTEPAD:
Code:
<?xml version="1.0"?><!--File Exported on 20/05/2010 12:26:01 p.m.--><table><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>01/03/2010</OrdDate><ReqDate>02/03/2010</ReqDate><ShipDate>03/03/2010</ShipDate><TransFee>7555.55</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>03/03/2010</OrdDate><ReqDate>04/03/2010</ReqDate><ShipDate>05/03/2010</ShipDate><TransFee>123.45</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>
Here are the full coding that generate XML File with hardcoding FolderPath instead of using User Prompted option.
Code:
using system.sqlclient
using System.Xml
[LEFT]private void FCreateXMLFile()
{ //Create XML file ....using DataReader
string strPathName = this.textboxXMLPath + this.textboxXMLName.Text;
string strSql;
strSql = "Select CustomerID, ShipName as CompanyName, "
+ " Convert(varchar(10),OrderDate,103) as OrdDate, "
+ " Convert(varchar(10), RequiredDate, 103) as ReqDate, "
+ " Convert(varchar(10), ShippedDate, 103) as ShipDate, "
+ " Convert(Numeric(10,2), Freight ) as TransFee "
+ " From TestOrders "
+ " Where ( OrderDate between @sdate and @edate ) "
+ " And (CustomerID = '" + strCustomerID + "')"
+ " Order By OrderDate ";
sqlconn = new SqlConnection(connstr);
sqlcmd = new SqlCommand(strSql, sqlconn);
sqlcmd.Parameters.Add("@sdate", SqlDbType.DateTime).Value = DateTime.Parse(lblDateFrom.Text);
sqlcmd.Parameters.Add("@edate", SqlDbType.DateTime).Value = DateTime.Parse(lblDateTo.Text);
sqlconn.Open();
DR = sqlcmd.ExecuteReader();
// XmlTextWriter XmlWriter = new XmlTextWriter(strPathName,System.Text.Encoding.UTF8 ); //<--- NOT WORKING
XmlTextWriter XmlWriter = new XmlTextWriter(@"F:\\TestXML\\CsharpElement.xml", System.Text.Encoding.UTF8);
XmlDocument XMLDOC = new XmlDocument();
try
{
XmlWriter.WriteStartDocument();
XmlWriter.WriteComment("File Exported on " + DateTime.Now);
XmlWriter.WriteStartElement("table");
if (this.RadiobuttonElement.Checked == true) // XML element format
{
int i = 0;
while (DataRead.Read())
{
XmlWriter.WriteStartElement("row");
for (i = 0; i < DR.FieldCount; i++)
{
XmlWriter.WriteStartElement(DR.GetName(i));
XmlWriter.WriteString(DR.GetValue(i).ToString());
XmlWriter.WriteEndElement();
}
XmlWriter.WriteEndElement();
}
XmlWriter.WriteEndElement();
}
if (this.RadiobuttonAttribute.Checked == true) //XML attribute format
{
int x = 0;
while (DataRead.Read())
{
XmlWriter.WriteStartElement("row");
for (x = 0; x < DR.FieldCount; x++)
{
XmlWriter.WriteAttributeString(DR.GetName(x), DR.GetValue(x).ToString());
}
XmlWriter.WriteEndElement();
}
XmlWriter.WriteEndElement();
}
XmlWriter.WriteEndElement();
XmlWriter.WriteEndDocument();
}
catch (Exception Ex)
{ MessageBox.Show(Ex.Message); }
finally
{
XmlWriter.Close();
DR.Close();
sqlconn.Close();
MessageBox.Show("Export to XML Completed", "XML EXPORT", MessageBoxButtons.OK);
}
}
[B]Here is the result of XML file open using NOTEPAD, the display is very bad:[/B]
<?xml version="1.0"?><!--File Exported on 20/05/2010 12:26:01 p.m.--><table><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>01/03/2010</OrdDate><ReqDate>02/03/2010</ReqDate><ShipDate>03/03/2010</ShipDate><TransFee>7555.55</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>03/03/2010</OrdDate><ReqDate>04/03/2010</ReqDate><ShipDate>05/03/2010</ShipDate><TransFee>123.45</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>03/03/2010</OrdDate><ReqDate>04/03/2010</ReqDate><ShipDate>05/03/2010</ShipDate><TransFee>987.65</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>07/03/2010</OrdDate><ReqDate>08/03/2010</ReqDate><ShipDate>09/03/2010</ShipDate><TransFee>55.00</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>15/04/2010</OrdDate><ReqDate>16/04/2010</ReqDate><ShipDate>17/04/2010</ShipDate><TransFee>123.45</TransFee></row><row><CustomerID>CHOPS</CustomerID><CompanyName>Chop-suey Chinese</CompanyName><OrdDate>23/12/2010</OrdDate><ReqDate>24/12/2010</ReqDate><ShipDate>25/12/2010</ShipDate><TransFee>75.25</TransFee></row></table>
[/LEFT]
Comment