Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while saving my textbox data to the xml file I have getting the error

Invalid name character in 'sdf f'. The ' ' character, hexadecimal value 0x20, cannot be included in a name.

Here is my code

Private Sub Cmd_Save_As_XML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd_Save_As_XML.Click

        Dim settings As New XmlWriterSettings()
        settings.Indent = True


        ' Initialize the XmlWriter.
        Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\00temp\GoogleEarth_VaneTest.xml", settings)

        With XmlWrt

            ' Write the Xml declaration.
            .WriteStartDocument()

            ' Write a comment.
            .WriteComment("XML Database.")

            ' Write the root element.
            .WriteStartElement("Google")

            .WriteStartElement("name")
            .WriteStartElement(Txt_Name.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("description")
            .WriteStartElement(Txt_Description.Text.ToString())
            .WriteEndElement()

            .WriteEndElement()

            
            ' Start our first lookat.
            .WriteStartElement("LookAt")

            ' The lookat nodes.

            .WriteStartElement("longitude")
            .WriteString(Txt_Longitude.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("latitude")
            .WriteString(+Txt_Latitude.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("altitude")
            .WriteString(Txt_Altitude.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("heading")
            .WriteString(Txt_Heading.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("titl")
            .WriteString(Txt_Tilt.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("range")
            .WriteString(Txt_Range.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("gxaltitudeMode")
            .WriteString(Txt_Altitude_Mode.Text.ToString())
            .WriteEndElement()

            ' The end of this lookat.

            .WriteEndElement()

            .WriteStartElement("styleUrl")
            .WriteStartElement(Txt_Styleurl.Text.ToString())
            .WriteEndElement()


            ' Start our first point.
            .WriteStartElement("Point")


            .WriteStartElement("gxdrawOrder")
            .WriteString(Txt_Draw_Order.Text.ToString())
            .WriteEndElement()

            .WriteStartElement("coordinates")
            .WriteString(Txt_Longitude.Text.ToString() + Txt_Latitude.Text.ToString() + Txt_Altitude.Text.ToString())
            .WriteEndElement()

            ' The end of this point.
            .WriteEndElement()


            ' Close the XmlTextWriter.
            .WriteEndDocument()
            .Close()

        End With

        MessageBox.Show("XML file saved successfully. In C:\00temp\")



    End Sub



Please Guide

once the textbox value is with space for example ben singh it may thows erros as mentioned above.
Posted
Comments
[no name] 3-Nov-14 7:15am    
Yes, it will throw error. You're trying to create an element with a name that has spaces, and that's not valid in XML. Element names can't contain spaces.
Bensingh 3-Nov-14 7:19am    
is it any solution for this

1 solution

Convert all spaces to underscore characters:
C#
mystring = mystring.Replace(" ", "_");
If you do this whenever you are handling user input and reverse it on presentation back, the user won't even notice the difference!
 
Share this answer
 
Comments
[no name] 3-Nov-14 7:25am    
Yeah!! was about to suggest that as a solution !
Good one :)

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