473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Creating XML File

126 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. <?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.

Expand|Select|Wrap|Line Numbers
  1. using system.sqlclient
  2. using System.Xml
  3.  
  4.  
  5. private void FCreateXMLFile()
  6. {   //Create XML file ....using DataReader
  7.  
  8.     string strPathName =  this.textboxXMLPath + this.textboxXMLName.Text;
  9.  
  10.     string strSql; 
  11.     strSql  = "Select CustomerID, ShipName as CompanyName, " 
  12.                 +  " Convert(varchar(10),OrderDate,103) as OrdDate, " 
  13.                 + " Convert(varchar(10), RequiredDate, 103) as ReqDate, " 
  14.                 + " Convert(varchar(10), ShippedDate, 103) as ShipDate, " 
  15.                 + " Convert(Numeric(10,2), Freight ) as TransFee " 
  16.                 + " From TestOrders "  
  17.                 + " Where ( OrderDate between @sdate and @edate ) " 
  18.                 +  " And (CustomerID = '" + strCustomerID + "')" 
  19.                 +  " Order By OrderDate ";
  20.  
  21.      sqlconn = new SqlConnection(connstr);
  22.      sqlcmd = new SqlCommand(strSql, sqlconn);           
  23.      sqlcmd.Parameters.Add("@sdate", SqlDbType.DateTime).Value = DateTime.Parse(lblDateFrom.Text);  
  24.      sqlcmd.Parameters.Add("@edate", SqlDbType.DateTime).Value = DateTime.Parse(lblDateTo.Text);
  25.      sqlconn.Open();
  26.  
  27.      DR = sqlcmd.ExecuteReader();
  28.  
  29.       //  XmlTextWriter XmlWriter = new XmlTextWriter(strPathName,System.Text.Encoding.UTF8 );    //<--- NOT WORKING    
  30.          XmlTextWriter XmlWriter = new XmlTextWriter(@"F:\\TestXML\\CsharpElement.xml", System.Text.Encoding.UTF8);
  31.          XmlDocument XMLDOC = new XmlDocument();
  32.       try
  33.             {
  34.  
  35.                 XmlWriter.WriteStartDocument();
  36.                 XmlWriter.WriteComment("File Exported on " + DateTime.Now);
  37.                 XmlWriter.WriteStartElement("table");                        
  38.  
  39.                if (this.RadiobuttonElement.Checked == true)     // XML element format
  40.                         {
  41.                             int i = 0;
  42.                             while (DataRead.Read())
  43.                             {
  44.                                  XmlWriter.WriteStartElement("row");
  45.                                  for (i = 0; i < DR.FieldCount; i++)
  46.                                    {
  47.                                               XmlWriter.WriteStartElement(DR.GetName(i));
  48.                                               XmlWriter.WriteString(DR.GetValue(i).ToString());
  49.                                               XmlWriter.WriteEndElement();
  50.                                     }
  51.                                     XmlWriter.WriteEndElement();
  52.                               }
  53.                                XmlWriter.WriteEndElement();  
  54.                         }
  55.  
  56.                   if (this.RadiobuttonAttribute.Checked == true)    //XML attribute format
  57.                           {
  58.                                    int x = 0;
  59.                                     while (DataRead.Read())
  60.                                        {
  61.                                              XmlWriter.WriteStartElement("row");
  62.                                              for (x = 0; x < DR.FieldCount; x++)
  63.                                                {
  64.              XmlWriter.WriteAttributeString(DR.GetName(x), DR.GetValue(x).ToString());
  65.                                                 }
  66.                                                    XmlWriter.WriteEndElement();
  67.                                          }
  68.                                         XmlWriter.WriteEndElement();
  69.                            }
  70.  
  71.                 XmlWriter.WriteEndElement();
  72.                 XmlWriter.WriteEndDocument();
  73.  
  74.             }
  75.             catch (Exception Ex)
  76.                     {  MessageBox.Show(Ex.Message); }
  77.  
  78.             finally
  79.                     {                    
  80.                         XmlWriter.Close();
  81.                         DR.Close();
  82.                         sqlconn.Close();
  83.  
  84.                         MessageBox.Show("Export to XML Completed", "XML EXPORT", MessageBoxButtons.OK);
  85.                     }
  86.         } 
  87.  
  88.  
  89. Here is the result of XML file open using NOTEPAD, the display is very bad:
  90. <?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>
  91.  
May 20 '10 #1
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.
May 20 '10 #2
tlhintoq
3,525 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. string myFolderPath
  2. {
  3.    get;
  4.    set; // You can also do some more advanced verification of the folder's existence here.
  5. }
  6.  
  7. void SelectSaveFolder()
  8. {
  9.    FolderBrowserDialog myFBD = new FBD();
  10.    DialogResult dr = myFBD.Show();
  11.    if (dr == DialogResult.OK)
  12.    {
  13.       // The user didn't cancel the dialog
  14.       myFolderPath = myFBD.FilePath();
  15.    }
  16. }
May 20 '10 #3
lenniekuah
126 New Member
@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.
May 20 '10 #4
lenniekuah
126 New Member
@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;
May 20 '10 #5
ThatThatGuy
449 Recognized Expert Contributor
@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
May 20 '10 #6
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.
May 20 '10 #7
tlhintoq
3,525 Recognized Expert Specialist
And just to lighten things up... Being from N.Zed you should get this...

Expand|Select|Wrap|Line Numbers
  1. string Baaah = "No";
May 20 '10 #8
lenniekuah
126 New Member
@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.
May 20 '10 #9
lenniekuah
126 New Member
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";
May 20 '10 #10
ThatThatGuy
449 Recognized Expert Contributor
@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...
May 20 '10 #11
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' "
May 20 '10 #12
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.
May 20 '10 #13
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Are you running the app in Windows? Then please take care of \ and / for path. This may create problems.

Regards
Dheeraj Joshi
May 20 '10 #14
lenniekuah
126 New Member
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.
May 21 '10 #15
lenniekuah
126 New Member
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

Expand|Select|Wrap|Line Numbers
  1. ----------------------------------------------------------------
  2.  private void btnGetPath_Click(object sender, EventArgs e)
  3.  {  //using FolderBrowserDialog control to retrieve folder name
  4.     try
  5.        {
  6.             this.folderBrowserDialog1.Description = "Select Folder Name";
  7.             this.folderBrowserDialog1.ShowDialog();
  8.  
  9.             if (this.folderBrowserDialog1.SelectedPath != "")
  10.              {
  11.                  this.txtXMLPath.Text = this.folderBrowserDialog1.SelectedPath + "\\";
  12.                  strPathName = this.folderBrowserDialog1.SelectedPath + "\\";
  13.              }
  14.         }
  15.         catch (Exception Ex)
  16.             {
  17.                 MessageBox.Show(Ex.Message, "Folder Dialog Problem", MessageBoxButtons.OK);
  18.             }            
  19.   } 
  20.  
  21. ----------------------------------------------------------------
  22. private string strPathName;
  23. private int i;
  24. private int x;
  25.  
  26.  
  27.   private void FCreateXMLFile()
  28.   {   //Create XML file ....using DataReader
  29.  
  30.     strPathName += this.txtXMLName.Text;      
  31.     string strSql;
  32.             strSql  = "Select CustomerID, ShipName as CompanyName, " 
  33.                      + " From  Orders "  
  34.                      + " Where ( CustomerID = 'CHOPS' ) "
  35.                      + " Order by CustomerID";
  36.  
  37.    sqlconn = new SqlConnection(connstr);
  38.    sqlcmd = new SqlCommand(strSql, sqlconn);
  39.  
  40.    sqlcmd.Parameters.Add("@sdate", SqlDbType.DateTime).Value = DateTime.Parse(lblDateFrom.Text);
  41.    sqlcmd.Parameters.Add("@edate", SqlDbType.DateTime).Value = DateTime.Parse(lblDateTo.Text);
  42.  
  43.    sqlconn.Open();
  44.    DR = sqlcmd.ExecuteReader();
  45.   XmlTextWriter XmlWriter = new XmlTextWriter(strPathName,System.Text.Encoding.UTF8 );  //<---   WORKING          
  46.  
  47.    try
  48.       {
  49.           XmlWriter.Formatting = Formatting.Indented;
  50.           XmlWriter.IndentChar = '\t';          
  51.  
  52.           XmlWriter.WriteStartDocument();
  53.           XmlWriter.WriteComment("File Exported on " + DateTime.Now);
  54.           XmlWriter.WriteStartElement("table");
  55.  
  56.           while (DR.Read())   
  57.            { 
  58.                int i = 0;
  59.                if (this.RadioButtonElement.Checked == true) // XML element format
  60.                {
  61.                   XmlWriter.WriteStartElement("row");
  62.                   for (i = 0; i < DR.FieldCount; i++)
  63.                    {
  64.                       XmlWriter.WriteStartElement(DR.GetName(i));
  65.                       XmlWriter.WriteString(DR.GetValue(i).ToString());
  66.                       XmlWriter.WriteEndElement();
  67.                    }
  68.                    XmlWriter.WriteEndElement();
  69.  
  70.               }
  71.              else   //XML attribute format
  72.                  {
  73.                    int x = 0;
  74.                    for (x = 0; x < DR.FieldCount; x++)
  75.                       {
  76.                         XmlWriter.WriteAttributeString(DR.GetName(x), DR.GetValue(x).ToString());
  77.                       }
  78.                       XmlWriter.WriteEndElement();
  79.                  }
  80.              } //end while
  81.              XmlWriter.WriteEndElement();
  82.             XmlWriter.WriteEndDocument();
  83.  
  84.      }
  85.      catch (Exception Ex)
  86.       {
  87.           MessageBox.Show(Ex.Message);
  88.       }
  89.  
  90.    finally
  91.      {
  92.          XmlWriter.Close();
  93.          DR.Close();
  94.          sqlconn.Close();
  95.          MessageBox.Show("Export to XML Completed", "XML EXPORT", MessageBoxButtons.OK);
  96.      }
  97. }   
May 21 '10 #16
ThatThatGuy
449 Recognized Expert Contributor
@lenniekuah
Glad, You worked it out
May 21 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
10815
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...
1
2479
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...
4
3427
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...
11
4982
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...
1
2201
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...
3
2659
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...
1
2113
by: j7o0s5 | last post by:
How to create file help chm?
2
1841
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...
0
1398
Kosal
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...
0
7171
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...
0
7388
Oralloy
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,...
0
7545
jinu1996
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...
0
7539
tracyyun
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...
0
5692
agi2029
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,...
0
3240
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...
0
1605
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 ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.