Help
-
Hi I have created a C# XML Reader and was wondering if someone could have a look at the code to see if I have done it right. Thanks Andrew ------------------------------- using System; using System.Xml; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyProject { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { System.IO.StreamReader stream = new System.IO.StreamReader ("About.xml"); XmlTextReader reader = null; reader = new XmlTextReader (stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an Element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display end of element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } } } }
Andrew McIntyre
-
Hi I have created a C# XML Reader and was wondering if someone could have a look at the code to see if I have done it right. Thanks Andrew ------------------------------- using System; using System.Xml; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyProject { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { System.IO.StreamReader stream = new System.IO.StreamReader ("About.xml"); XmlTextReader reader = null; reader = new XmlTextReader (stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an Element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display end of element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } } } }
Andrew McIntyre
Hi Andrew. This isn't the best way to ask a question on CodeProject. What you'll want to do instead is try out your code and if it isn't working the way you expect or want, ask a specific question that targets what is going wrong (for example, it's better for volunteers to read through four lines of code that target where you think the problem is, rather than go through a full dump of code... and if you make it easier for volunteers to help you, they will!) [EDIT] - oh, and it's never a good idea to use "Help" for the subject line. Everyone already knows you're looking for help, otherwise you wouldn't be posting here. A subject line that helps focus on the specific question you have in mind is much better and easier for volunteers to work with.
-
Hi I have created a C# XML Reader and was wondering if someone could have a look at the code to see if I have done it right. Thanks Andrew ------------------------------- using System; using System.Xml; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyProject { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { System.IO.StreamReader stream = new System.IO.StreamReader ("About.xml"); XmlTextReader reader = null; reader = new XmlTextReader (stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an Element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display end of element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } } } }
Andrew McIntyre
Everytime I debug the project and when the client appear the XML document that I want to appear doesn't appear on screen for some reason. I get no error in the error list and thats why I have asked you guy's. Andrew
Andrew McIntyre
-
Everytime I debug the project and when the client appear the XML document that I want to appear doesn't appear on screen for some reason. I get no error in the error list and thats why I have asked you guy's. Andrew
Andrew McIntyre
Well, running under the assumption that your about.xml file isn't empty, you may want to handle the other node types in your switch statement as you could be skipping the content. The MSDN documentation has a good sample that is pretty close to what you coded, and includes the other node types. http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.read.aspx[^]
-
Hi I have created a C# XML Reader and was wondering if someone could have a look at the code to see if I have done it right. Thanks Andrew ------------------------------- using System; using System.Xml; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyProject { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { System.IO.StreamReader stream = new System.IO.StreamReader ("About.xml"); XmlTextReader reader = null; reader = new XmlTextReader (stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an Element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display end of element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } } } }
Andrew McIntyre
-
Hi I have created a C# XML Reader and was wondering if someone could have a look at the code to see if I have done it right. Thanks Andrew ------------------------------- using System; using System.Xml; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyProject { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { System.IO.StreamReader stream = new System.IO.StreamReader ("About.xml"); XmlTextReader reader = null; reader = new XmlTextReader (stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an Element. Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read attributes. Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); Console.WriteLine(">"); break; case XmlNodeType.Text: //Display the text in each element. Console.WriteLine (reader.Value); break; case XmlNodeType. EndElement: //Display end of element. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } } } }
Andrew McIntyre
Well I downloaded your code and it works fine. EXCEPT, this is a WinForms application and you are displaying the information via Console.WriteLine so all the output is going to the console output window rather than being displayed in your windows form. I would suggest that you spend some more time learning how to use WinForms applications before proceeding further.