Problem Creating XML File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenniekuah
    New Member
    • Oct 2006
    • 126

    Problem Creating XML File

    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:
    Code:
    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:
    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]
    Last edited by Niheel; May 20 '10, 05:58 AM. Reason: please use code tags to show code
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Code:
      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.
      Code:
      string strPathName =  this.textboxXMLPath [U][highlight][B]+ "\\"[/B][/highlight] [/U]+ 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
      Code:
      string strPathName =  this.textboxXMLPath
      Textboxes are not there to hold your variables for you. When you are done getting the folderpath from the FolderBrowserDi alog you should put it in a property.

      Code:
      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();
         }
      }

      Comment

      • lenniekuah
        New Member
        • Oct 2006
        • 126

        #4
        Originally posted by tlhintoq
        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.
        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.

        Comment

        • lenniekuah
          New Member
          • Oct 2006
          • 126

          #5
          Originally posted by tlhintoq
          Code:
          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.
          Code:
          string strPathName =  this.textboxXMLPath [U][highlight][B]+ "\\"[/B][/highlight] [/U]+ 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
          Code:
          string strPathName =  this.textboxXMLPath
          Textboxes are not there to hold your variables for you. When you are done getting the folderpath from the FolderBrowserDi alog you should put it in a property.

          Code:
          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();
             }
          }
          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 FolderBrowserDi alog:

          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:


          XmlTextWriter XmlWriter = new XmlTextWriter(s trPathName,Syst em.Text.Encodin g.UTF8 );

          This is the coding extracted from the earlier posting above which show the variables containing the Folder Name from the FolderBrowserDi alog prompting and the TextBox which the user input the XML File name.
          string strPathName = this.textboxXML Path + this.textboxXML Name.Text;
          Last edited by lenniekuah; May 20 '10, 05:56 AM. Reason: extraction from posting

          Comment

          • ThatThatGuy
            Recognized Expert Contributor
            • Jul 2009
            • 453

            #6
            Originally posted by lenniekuah
            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:
            Code:
            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:
            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]
            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

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              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 BrowseFolderDia log 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.textboxXML Path + this.textboxXML Name.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.textboxXML Path + this.textboxXML Name.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.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                And just to lighten things up... Being from N.Zed you should get this...

                Code:
                string Baaah = "No";

                Comment

                • lenniekuah
                  New Member
                  • Oct 2006
                  • 126

                  #9
                  Originally posted by ThatThatGuy
                  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
                  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.

                  Comment

                  • lenniekuah
                    New Member
                    • Oct 2006
                    • 126

                    #10
                    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\\Cu stomer.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";

                    Comment

                    • ThatThatGuy
                      Recognized Expert Contributor
                      • Jul 2009
                      • 453

                      #11
                      Originally posted by lenniekuah
                      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\\Cu stomer.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";
                      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.Sav e()..
                      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...

                      Comment

                      • tlhintoq
                        Recognized Expert Specialist
                        • Mar 2008
                        • 3532

                        #12
                        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' "

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          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.

                          Comment

                          • Dheeraj Joshi
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1129

                            #14
                            Are you running the app in Windows? Then please take care of \ and / for path. This may create problems.

                            Regards
                            Dheeraj Joshi

                            Comment

                            • lenniekuah
                              New Member
                              • Oct 2006
                              • 126

                              #15
                              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.

                              Comment

                              Working...