Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

XMLReader class

xml xmlreader

  • Please log in to reply
9 replies to this topic

#1 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 12 October 2012 - 02:31 PM

I have XML file that I have write with XMLWritter class within my project. How to read that XML file in console?
I have try many codes but they are not working.
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#2 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 12 October 2012 - 02:33 PM

Can you just use DataSet and use method like LoadXML or ReadXML ... then it returns a Dataset ... then you can parse it from there through rows and columns. It has been a while for me to play with xml.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#3 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 12 October 2012 - 02:36 PM

I was last playing with XML before one year. I forget 90% of everything.
Can you give me an example?
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#4 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 12 October 2012 - 02:42 PM

Here is an example:
http://support.microsoft.com/kb/311566
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#5 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 12 October 2012 - 02:51 PM

Not helpfull. Let me specify my need.
First, I need to read the name of the XML document,
second, I need to read root element,
third, I need to read attribute elements and their values.

Those 3 things need to be written in console separated.
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#6 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 12 October 2012 - 03:26 PM

Dataset should contains everything you need. All you have to do is to debug or loop through and print out the column name, and look in the debug table. Your root should be the Datatable.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#7 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 12 October 2012 - 04:06 PM

XmlDocument doc = new XmlDocument();
		 doc.Load("employees.xml");
		 XmlNodeList customers = null;
		 customers = doc.SelectNodes("Employees");
		 foreach (XmlNode customer in customers)
		 {
			 Console.WriteLine("ID = {0}, Ime = {1}, Prezime = {2}", customer.Attributes["ID"].Value, customer.Attributes["FirstName"].Value, customer.Attributes["LastName"].Value);
		 }
		 Console.ReadKey();

This is the code I found. It is easier to work than with DataSet and DataTable.
The only problem is this:

Posted Image
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#8 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 12 October 2012 - 04:38 PM

Then I guess all you have to do is write a wrapper ... eg: printValue(object value)

if (value != null) { // print it out ... }

then wrap around all the column names that you want to print it out.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#9 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 12 October 2012 - 05:06 PM

try
		 {
			 XmlDocument doc = new XmlDocument();
			 doc.Load("employees.xml");
			 XmlNodeList customers = null;
			 customers = doc.SelectNodes("Employees");
			 if (customers != null)
			 {
				 foreach (XmlNode customer in customers)
				 {
					 if (customers != null)
					 {
						 Console.WriteLine("ID = {0}, Ime = {1}, Prezime = {2}", customer.Attributes["ID"].Value, customer.Attributes["FirstName"].Value, customer.Attributes["LastName"].Value);
					 }
				 }
			 }
		 }
		 catch (Exception e)
		 {
			 Console.WriteLine(e.ToString());
		 }
		 Console.ReadKey();

Posted Image

Line 28 is where I print out values of nodes. And if I make like this:

if (customer != null)
{
...
}

then I get the same exception but this time for line 26 which is if statement.
And if I print out only customer.Name then application is executing normaly and I get the name of the node.
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#10 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 12 October 2012 - 05:56 PM

I meant this:
printValue(customer.Attributes["ID"].Value)

and do the rest for all of them.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download