I am trying to using C#Net2008 to create XML File.
--------------------------------------------------
The User are prompted by folderBrowserDialog 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: -
XmlTextWriter XmlWriter = new XmlTextWriter(strPathName,System.Text.Encoding.UTF8 );
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: - <?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. -
using system.sqlclient
-
using System.Xml
-
-
-
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);
-
}
-
}
-
-
- Here is the result of XML file open using NOTEPAD, the display is very bad:
-
<?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>
-
-
16 4665 tlhintoq 3,525
Recognized Expert Specialist Original Poster: I have xxx, then do yyy then zzz happens. Here's my code ...
Ok. You know what you have.
What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything, or exception or description of any error messages you are getting. You haven't described anything that is broken, or any 'expected' results versus 'actual' results.
I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.
tlhintoq 3,525
Recognized Expert Specialist - string strPathName = this.textboxXMLPath + this.textboxXMLName.Text;
Since nobody here knows what your textboxes contain there is little anyone can tell you except: You screwed up the path.
Put a breakpoint on this line and walk through it line by line with the F10 key.
Look at the values of the variables and see where you went wrong.
In something like this I generally forget to add a folder separator when adding variable together. - string strPathName = this.textboxXMLPath + "\\" + this.textboxXMLName.Text;
but maybe you have an extra "\" at the start of the path, or you trimmed off the volume "C:", or you replaced all the "\" with "/". There are hundreds of things that could be wrong with the path to make it invalid but you should be able to tell by looking at them in the Locals or Autos pallet while you step through the code.
Personally, I avoid this type of stuff - string strPathName = this.textboxXMLPath
Textboxes are not there to hold your variables for you. When you are done getting the folderpath from the FolderBrowserDialog you should put it in a property. - string myFolderPath
-
{
-
get;
-
set; // You can also do some more advanced verification of the folder's existence here.
-
}
-
-
void SelectSaveFolder()
-
{
-
FolderBrowserDialog myFBD = new FBD();
-
DialogResult dr = myFBD.Show();
-
if (dr == DialogResult.OK)
-
{
-
// The user didn't cancel the dialog
-
myFolderPath = myFBD.FilePath();
-
}
-
}
@tlhintoq
Hi tlhintoq
You are wrong to say this : What you don't have is a question
I did highlight the error message in my posting : here is the extraction from my posting : Error Message:
The Given path's format is not supported. @tlhintoq
Hi tlhintoq
As a Moderator, You are wrong again to make this statement. It shows that you did not read the C# Coding.
Here is your statement extracted from your posting: Since nobody here knows what your textboxes contain there is little anyone can tell you except: You screwed up the path.
This is my original posting from above descripting the prompting using FolderBrowserDialog: The User are prompted by folderBrowserDialog control to select the folder to store the XML File. But the variable which contained the folder path generated this error message: XmlTextWriter XmlWriter = new XmlTextWriter(strPathName,System.Text.Encoding.UTF 8 );
This is the coding extracted from the earlier posting above which show the variables containing the Folder Name from the FolderBrowserDialog prompting and the TextBox which the user input the XML File name. string strPathName = this.textboxXMLPath + this.textboxXMLName.Text; @lenniekuah
What kind of xml file is that.... its all messed up .. there's no root node...
XmlReader or XmlDocument will definitely raise errors on such encounters
tlhintoq 3,525
Recognized Expert Specialist lenniekuah: Hi tlhintoq
You are wrong to say this : What you don't have is a question
I did highlight the error message in my posting : here is the extraction from my posting :
Error Message:
The Given path's format is not supported.
Right. You highlighted an error message. What is your QUESTION? Yeah know, a question starts with Who, What, Where, When, Why and sometimes how... ends with a question mark. (?) We can guess what you might be wondering based on an error message... but why should all the volunteers here have to guess at what you are looking for? - What would make a path invalid?
- Which property from a BrowseFolderDialog should I be using?
- Can I use an IP address as part of the path?
- My finished filename path becomes "C;\\root*\bob\". What part of that is bad?
Repeating your earlier statements in BOLD really doesn't help. It's like someone repeating the exact same sentence only louder. It does not add any new information. string strPathName = this.textboxXMLPath + this.textboxXMLName.Text;
As I said before, nobody here knows what the contents of your textboxes are. So how can we help you figure out why string strPathName = this.textboxXMLPath + this.textboxXMLName.Text;
is wrong? For all we know textboxXMLPath contains "Fred" and TextBoxXMLName.Text contains "Barney"
I gave you several suggestions as to what might cause your path to be formatted wrong. Did you check them? Did you put in the breakpoint, walk through your code and check the variables values? How about this... Walk through your code again and this time copy the path that Visual Studio is saying is formatted incorrectly and paste it here. If we can't see what you see, then we can't give you more targeted help.
tlhintoq 3,525
Recognized Expert Specialist
And just to lighten things up... Being from N.Zed you should get this... @ThatThatGuy
Hi ThatThatGuy,
Thank you very much for your help. You are just awesome helper.
The XML file is using ELEMENT.
I did not use XMLReader. I am using XMLWriter to create XML File of Element format.
Who do you mean by this: there's no root node...
The coding that I posted works very well in C#Net2003 but under C#Net2008 it is not.
Hi tlhintog,
Under the DEBUG Immediate Window I did this to extract the content of variable strPathName:
?strPathName
"System.Windows.Forms.TextBox, Text: F:\\TESTXML\\Customer.XML"
I am very confused of what you are trying to suggest. Here are the quotation from your response: Being from N.Zed you should get this... string Baaah = "No"; @lenniekuah
Ok.... i read your first post....
there're few corrections to do....
The first error you're getting is because your path format may be incorrect...
either the folder path doesn't exists or or there's some mismatch in the path.... with respect to back slashes... check on debug for the valid file path
The second problem can be resolved if you construct youre Xml document using Linq classes for xml... (if you're using .net 3.5 )...
Linq provides very good classes for writing xml files.....
there's isn't any alignment or format problem when any xml file is written via Linq classes...
i've no idea working on XmlWriter.... or you can also use XmlDocument.Save()..
to save your constructed document....
instead of XmlWriter..... i've been using that since for a while.... there doesnt't seem to be such alignment problems...
tlhintoq 3,525
Recognized Expert Specialist
I am very confused of what you are trying to suggest. Here are the quotation from your response:
Being from N.Zed you should get this...
string Baaah = "No";
I was just trying to lighten things up. Just a joke from having lived in Australia. " 'baahhhh' means 'no' "
tlhintoq 3,525
Recognized Expert Specialist Error: The Given path's format is not supported.
ThatThatGuy: The first error you're getting is because your path format may be incorrect...
either the folder path doesn't exists or or there's some mismatch in the path.... with respect to back slashes... check on debug for the valid file path
The error message does NOT indicate that the folder isn't found / doesn't exist. It means exactly what it says: The format is unsupported. In other words it is completely unrecognizable as a path at all.
Are you running the app in Windows? Then please take care of \ and / for path. This may create problems.
Regards
Dheeraj Joshi
Hi dheeraj
The problem is from Window Application written using C#NET2008.
In C# have to use double "\\" which represent single \.
In VB then single \. not double "\\"
The coding that I encounter problem works well in C#NET2003 but my company migrate the application to C#.NET2008.
Hi tlhintog, ThatThatGuy, Dheeraj Yaaa.....hooooo..............Yeee........Haaaaa... ..........
Thanks to all of you for your suggestions.
I have recreated the coding and now is working very well and produced very nice Element Format XML File as well.
The solution is instead of copy the Folderpath to the variable strPathName, I used the FolderBrowserDialog control to write the folder path to the variable strPathName then after the user input the XMLFile name in the textboxXMLName I added it to strPathName. That solved the problem. I would like to share the coding with Newbies who may have similar problem creating XML File using SYSTEM.XML - ----------------------------------------------------------------
-
private void btnGetPath_Click(object sender, EventArgs e)
-
{ //using FolderBrowserDialog control to retrieve folder name
-
try
-
{
-
this.folderBrowserDialog1.Description = "Select Folder Name";
-
this.folderBrowserDialog1.ShowDialog();
-
-
if (this.folderBrowserDialog1.SelectedPath != "")
-
{
-
this.txtXMLPath.Text = this.folderBrowserDialog1.SelectedPath + "\\";
-
strPathName = this.folderBrowserDialog1.SelectedPath + "\\";
-
}
-
}
-
catch (Exception Ex)
-
{
-
MessageBox.Show(Ex.Message, "Folder Dialog Problem", MessageBoxButtons.OK);
-
}
-
}
-
-
----------------------------------------------------------------
-
private string strPathName;
-
private int i;
-
private int x;
-
-
-
private void FCreateXMLFile()
-
{ //Create XML file ....using DataReader
-
-
strPathName += this.txtXMLName.Text;
-
string strSql;
-
strSql = "Select CustomerID, ShipName as CompanyName, "
-
+ " From Orders "
-
+ " Where ( CustomerID = 'CHOPS' ) "
-
+ " Order by CustomerID";
-
-
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 ); //<--- WORKING
-
-
try
-
{
-
XmlWriter.Formatting = Formatting.Indented;
-
XmlWriter.IndentChar = '\t';
-
-
XmlWriter.WriteStartDocument();
-
XmlWriter.WriteComment("File Exported on " + DateTime.Now);
-
XmlWriter.WriteStartElement("table");
-
-
while (DR.Read())
-
{
-
int i = 0;
-
if (this.RadioButtonElement.Checked == true) // XML element format
-
{
-
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();
-
-
}
-
else //XML attribute format
-
{
-
int x = 0;
-
for (x = 0; x < DR.FieldCount; x++)
-
{
-
XmlWriter.WriteAttributeString(DR.GetName(x), DR.GetValue(x).ToString());
-
}
-
XmlWriter.WriteEndElement();
-
}
-
} //end while
-
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);
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Nicholas Then |
last post by:
I am writing an ASP.NET application and I have a class
that I have written to create a vCard...it just returns a
string with all the necessarry info... Anyway...is there
a way that I can create a...
|
by: egsys |
last post by:
I am simply trying to create a file in access 2003. Receive an error
message that:
"Workgroup administrator couldn't create the workgroup information
file. Make sure that you have specified a...
|
by: Bnob |
last post by:
It is possible to create a file in local user-machine?
With this code in code-behind:
fs = New System.IO.FileStream(lFileCSV, System.IO.FileMode.Create,
System.IO.FileAccess.Write)
The file...
|
by: Russ Green |
last post by:
I need help writing a method to create a simple text file. I want that file
to be saved to a local drive (client side of my asp.net app). The filename
will always be the same "license.txt" but the...
|
by: Victor |
last post by:
Hi,
We have following setup here Web Server (Win2000) and FTP Server (Win 2000)
We have ASP.NET application which runs on Web Server and writes file(s) onto
FTP Server. We created asp.net...
| |
by: christiang |
last post by:
Hi guys, I'd like to use Alexia thumbnail service. It's an interesting
service, you pass an url to its cgi and you get a thumbnail of that
url. Of course they don't have the screenshots of all the...
|
by: j7o0s5 |
last post by:
How to create file help chm?
|
by: SteMc |
last post by:
Hi, I'm trying to create a text file on an asp page but am having loads
of trouble.
At the minute what happens is this - I have a form with a textarea.
When the form posts, the action is to run...
|
by: Kosal |
last post by:
Hi
I have problem with vb.net I want to create file setup or exe file for run on Client Computer that no need to install vb.net 2005 but I never do so I don't know what can i do?
Please help...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |