Parsing XML file -exception because element node contains a space in name
-
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
-
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
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 !
-
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
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