Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Parsing XML file -exception because element node contains a space in name

Parsing XML file -exception because element node contains a space in name

Scheduled Pinned Locked Moved XML / XSL
helpcsharpvisual-studiocomdebugging
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    califgal
    wrote on last edited by
    #1

    I am trying to parse the below xml file <myStuff> <my Name>Tom Brown</myName> <my Telephone>123456789</myTelephone> <my Email>sample@testmail.com</myEmail> <my Age>22</myAge> <my Age>32</myAge> <my Sex>M</mySex> </myStuff> with following code. using (StreamWriter sw = new StreamWriter("result.txt", true)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. sw.Write("<" + reader.Name.ToString()); sw.Write(">"); break; case XmlNodeType.Text: //Display the text in each element. sw.Write(reader.Value); break; case XmlNodeType.EndElement: //Display the end of the element. sw.Write("</" + reader.Name); sw.Write(">"); break; } } but it throws an exception because the Node name contains whitespace can anyone help me fix this issue? ->the exception message is below. System.Xml.XmlException was unhandled Message="'>' is an unexpected token. The expected token is '='. Line 2, position 11." Source="System.Xml" LineNumber=2 LinePosition=11 SourceUri="file:///C:/Users/mirapark/Documents/Visual Studio 2008/Projects/simpleXMLparser/SimpleXMLParser/bin/Debug/sampleXML.xml" StackTrace: at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at WindowsFormsApplication1.Form1.xmlParse() in C:\Users\mirapark\Documents\Visual Studio 2008\Projects\IntellisenseFileTesting\IntellisenseFileTesting\Form1.cs:line 85 at WindowsFormsApplication1.Form1.button1_Click_1(Object sender, EventArgs e) in C:\Users\mirapark\Documents\Visual Studio 2008\Projects\IntellisenseFileTesting\IntellisenseFileTesting\Form1.cs:line 35 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, Mou

    S M 2 Replies Last reply
    0
    • C califgal

      I am trying to parse the below xml file <myStuff> <my Name>Tom Brown</myName> <my Telephone>123456789</myTelephone> <my Email>sample@testmail.com</myEmail> <my Age>22</myAge> <my Age>32</myAge> <my Sex>M</mySex> </myStuff> with following code. using (StreamWriter sw = new StreamWriter("result.txt", true)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. sw.Write("<" + reader.Name.ToString()); sw.Write(">"); break; case XmlNodeType.Text: //Display the text in each element. sw.Write(reader.Value); break; case XmlNodeType.EndElement: //Display the end of the element. sw.Write("</" + reader.Name); sw.Write(">"); break; } } but it throws an exception because the Node name contains whitespace can anyone help me fix this issue? ->the exception message is below. System.Xml.XmlException was unhandled Message="'>' is an unexpected token. The expected token is '='. Line 2, position 11." Source="System.Xml" LineNumber=2 LinePosition=11 SourceUri="file:///C:/Users/mirapark/Documents/Visual Studio 2008/Projects/simpleXMLparser/SimpleXMLParser/bin/Debug/sampleXML.xml" StackTrace: at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at WindowsFormsApplication1.Form1.xmlParse() in C:\Users\mirapark\Documents\Visual Studio 2008\Projects\IntellisenseFileTesting\IntellisenseFileTesting\Form1.cs:line 85 at WindowsFormsApplication1.Form1.button1_Click_1(Object sender, EventArgs e) in C:\Users\mirapark\Documents\Visual Studio 2008\Projects\IntellisenseFileTesting\IntellisenseFileTesting\Form1.cs:line 35 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, Mou

      S Offline
      S Offline
      Samer Aburabie
      wrote on last edited by
      #2

      Simply as you said it holds white spaces in the tag ! You can't have white spaces in the tag UNLESS you will be adding an attribute ... the tag name should always be one word whitespace-less !

      Sincerely Samer Abu Rabie Imagination is more important than knowledge !

      1 Reply Last reply
      0
      • C califgal

        I am trying to parse the below xml file <myStuff> <my Name>Tom Brown</myName> <my Telephone>123456789</myTelephone> <my Email>sample@testmail.com</myEmail> <my Age>22</myAge> <my Age>32</myAge> <my Sex>M</mySex> </myStuff> with following code. using (StreamWriter sw = new StreamWriter("result.txt", true)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. sw.Write("<" + reader.Name.ToString()); sw.Write(">"); break; case XmlNodeType.Text: //Display the text in each element. sw.Write(reader.Value); break; case XmlNodeType.EndElement: //Display the end of the element. sw.Write("</" + reader.Name); sw.Write(">"); break; } } but it throws an exception because the Node name contains whitespace can anyone help me fix this issue? ->the exception message is below. System.Xml.XmlException was unhandled Message="'>' is an unexpected token. The expected token is '='. Line 2, position 11." Source="System.Xml" LineNumber=2 LinePosition=11 SourceUri="file:///C:/Users/mirapark/Documents/Visual Studio 2008/Projects/simpleXMLparser/SimpleXMLParser/bin/Debug/sampleXML.xml" StackTrace: at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at WindowsFormsApplication1.Form1.xmlParse() in C:\Users\mirapark\Documents\Visual Studio 2008\Projects\IntellisenseFileTesting\IntellisenseFileTesting\Form1.cs:line 85 at WindowsFormsApplication1.Form1.button1_Click_1(Object sender, EventArgs e) in C:\Users\mirapark\Documents\Visual Studio 2008\Projects\IntellisenseFileTesting\IntellisenseFileTesting\Form1.cs:line 35 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, Mou

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        The code isn't at fault, the XML is not properly formed.

        --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups