12 Replies - 509 Views - Last Post: 30 May 2012 - 10:20 AM Rate Topic: -----

#1 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Reading from XMLs

Posted 29 May 2012 - 01:21 PM

Hello everyone!

I am currently new to XML and learning about it. (Which isn't too much of a problem.) However, I am having a very rough time with trying to use it with C#, I have looked at the tutorials.

Objective: I'm trying to be able to read my XML file and grab values that I call for.

Issue: It seems like every time I use it and call some stuff, then call something else, it just throws an error. I can't figure it out or understand how or why. I've tried multiple things.

XML Example:
<test>
	<!--<login>
		<username>tyler</username>
		<password>testing</password>
	</login>-->
	<register>
		<account>
			<email>test@test.com</email>
			<first>tyler</first>
			<last>yeary</last>
			<phone>4562134567</phone>
			<password>123abc</password>
			<verify>123abc</verify>
			<type>Hotel</type>
		</account>
		<hotel>
			<brand>(Independent)</brand>
			<name>YourCLS</name>
			<address1>1563 omdfg its here</address1>
			<address2>wtf?</address2>
			<city>hello!</city>
			<state>GB</state>
			<zip>51321</zip>
			<location>North</location>
			<segment>upscale</segment>
			<fax>1234567890</fax>
			<identity>check</identity>
		</hotel>
		<check>
			<amount>586.21</amount>
			<number>12596654</number>
		</check>
	</register>
</test>


Yes I know Login element is commented out. I've done that on purpose.


Code
//In my constructor:
class Register
    {
        String RegisterURL = "http://localhost:54288/User/Register";
        String HotelInfo = "http://localhost:54288/User/RegisterAssociation";
        String Email { get; set; }
        String FirstName { get; set; }
        String LastName { get; set; }
        String Phone { get; set; }
        String Pass1 { get; set; }
        String Pass2 { get; set; }
        String userStats { get; set; }
        Boolean testResults = false;
        List<String> LogData = new List<String>();
        IWebDriver driver;
        XmlNodeList XmlRegister;

        public Register(IWebDriver webDriver, XmlNodeList node)
        {
            driver = webDriver;
            XmlRegister = node;
            try
            {
            XmlNodeList copyXmlRegister = XmlRegister;

            foreach (XmlNode data in copyXmlRegister)
            {
                Email = data["email"].InnerText;
                FirstName = data["first"].InnerText;
                LastName = data["last"].InnerText;
                Phone = data["phone"].InnerText;
                Pass1 = data["password"].InnerText;
                Pass2 = data["verify"].InnerText;
                userStats = data["type"].InnerText;
            }



Then later on (split second) I go to another method within the same class. Note the XmlNodeList is global within the class.

public void RegisterTest1(XmlNodeList allNodes)
        {
            driver.Navigate().GoToUrl(RegisterURL);
            LogData.Add("Restarting Register page.");

            this.MainData();
            this.Wait(2);

            try
            {
                
                foreach (XmlNode info in allNodes)
                {
                    SelectElement HotelBrand = new SelectElement(driver.FindElement(By.Name("Hotel.BrandName")));
//This is line 134
                    HotelBrand.SelectByText(info["brand"].InnerText);

                    //HotelBrand.SelectByText("(Independent)");

                    IWebElement HotelName = driver.FindElement(By.Name("Hotel.HotelName"));
                    HotelName.SendKeys(info["name"].InnerText);

                    IWebElement Address1 = driver.FindElement(By.Name("Hotel.Address1"));
                    Address1.SendKeys(info["address1"].InnerText);

                    IWebElement Address2 = driver.FindElement(By.Name("Hotel.Address2"));
                    Address2.SendKeys(info["address2"].InnerText);




IWebElement is a different thing. please ignore them.


However, I keep getting an error like this:
Registration Test did not successfully complete. Exception thrown : System.NullReferenceException: Object reference not set to an instance of an object.
   at SeleniumTest.Register.RegisterTest1(XmlNodeList allNodes) in C:\Users\tylery\Documents\Visual Studio 2010\Projects\SeleniumTest\SeleniumTest\Register.cs:line 134


This post has been edited by Twister1002: 29 May 2012 - 01:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from XMLs

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from XMLs

Posted 29 May 2012 - 01:33 PM

"Null reference exception" - Very common with new programmers. I covered it in this article:
What does this error mean?

This looks like a whole lot of micromanaging of code and the WAY long way around to do this.

Just serialize/de-serialize your objects.
[*]Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 1
  • +
  • -

#3 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Reading from XMLs

Posted 29 May 2012 - 01:47 PM

View PosttlhIn`toq, on 29 May 2012 - 04:33 PM, said:

"Null reference exception" - Very common with new programmers. I covered it in this article:
What does this error mean?

This looks like a whole lot of micromanaging of code and the WAY long way around to do this.

Just serialize/de-serialize your objects.
[*]Separating data from GUI - PLUS - serializing the data to XML


Debugging isn't really the issue. I do understand the error: I've searched for it, and I know where it is. My only issue is I can't figure out why it's giving me a null value when there should be text in it. I've tried several different XPath ways to find it, and it all says the same thing. the commented line will work without issues. (Of course it would!)

I'm also not using a GUI. Its a Webform. It does look like a bunch of running round in a long way (more than it should) however, to do what I need to do, it is needed at least for now, and there is no cutting down.

This post has been edited by Twister1002: 29 May 2012 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from XMLs

Posted 29 May 2012 - 01:52 PM

View PostTwister1002, on 29 May 2012 - 02:47 PM, said:

Debugging isn't really the issue. I do understand the error: I've searched for it, and I know where it is. My only issue is I can't figure out why it's giving me a null value when there should be text in it.


Ok. Is the text null, or is the object HOLDING the text null?

My first guess would be the object holding the text went out of scope before you try to work with it.

In the code blocks you provided were exactly is the error, and what item is null?

Can you breakpoint it and take a screen shot showing the locals pallet so we can all see it?
Was This Post Helpful? 0
  • +
  • -

#5 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Reading from XMLs

Posted 29 May 2012 - 02:17 PM

View PosttlhIn`toq, on 29 May 2012 - 04:52 PM, said:

Ok. Is the text null, or is the object HOLDING the text null?

My first guess would be the object holding the text went out of scope before you try to work with it.

In the code blocks you provided were exactly is the error, and what item is null?

Can you breakpoint it and take a screen shot showing the locals pallet so we can all see it?



The object is unable to actually find it. I found out one reason why, but it broke another section of my code (which is this picture) >_<. I had it set to specifically only in the "account" node. I changed that back to the parent of that, and now its killing another section. It shows all the data, but for some reason it is just saying "no!".Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: Reading from XMLs

Posted 29 May 2012 - 02:18 PM

It becomes harder to debug things when you do multiple things on one line. I would make this:

var hotelBrandNameElement = driver.FindElement(By.Name("Hotel.BrandName"));
if (hotelBrandNameElement)
{
    SelectElement HotelBrand = new SelectElement(hotelBrandNameElement);
}
else
{
    throw new Exception("Failed to find Hotel.BrandName element");
}



EDIT: You need to learn to test things before you try to use them.

Email = data["Email"].InnerText


should be something like (C# is rusty):

if (data["Email"])
{
    Email = data["Email"].InnerText;
}
else
{
    Email = String.Empty; // or however you want to handle it
}


This is ESPECIALLY true if you're doing QA, which by virtue of the fact you're using Selenium I'd say you probably are.
Was This Post Helpful? 2
  • +
  • -

#7 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Reading from XMLs

Posted 29 May 2012 - 02:50 PM

View PostJackOfAllTrades, on 29 May 2012 - 05:18 PM, said:

It becomes harder to debug things when you do multiple things on one line. I would make this:

var hotelBrandNameElement = driver.FindElement(By.Name("Hotel.BrandName"));
if (hotelBrandNameElement)
{
    SelectElement HotelBrand = new SelectElement(hotelBrandNameElement);
}
else
{
    throw new Exception("Failed to find Hotel.BrandName element");
}



EDIT: You need to learn to test things before you try to use them.

Email = data["Email"].InnerText


should be something like (C# is rusty):

if (data["Email"])
{
    Email = data["Email"].InnerText;
}
else
{
    Email = String.Empty; // or however you want to handle it
}


This is ESPECIALLY true if you're doing QA, which by virtue of the fact you're using Selenium I'd say you probably are.


I have been testing them, other wise I wouldn't be posting something I didn't try in multiple ways. If my XML did have the node as "Email" (which it is "email") then it wouldn't have fixed anything.

Also good call, I am using Selenium.

But even then, I tested it all with Text before I started using XML. So I know it is something with the XML part that I am trying to incorporate. I just can't figure out where at in the XML I am having problems.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from XMLs

Posted 29 May 2012 - 03:01 PM

Twister1002 said:



A breakpoint stops execution at the start of the line, before that line is executed.
I would expect Email to be null at the START of line 58 if you didn't initialize it upon creation, because that line has not yet run and therefore no value has been applied yet.

Press F10 to execute this line and move to line 59. You should see that "Email" now has a value.

Its going to be very hard to debug if you don't understand the tools you're using.

Also, if you aren't initializing your variables/objects at the time you create them then you are just begging for these (null reference) problems.

string Email; // Bad
string Email = "unknown"; // Initial known value.  Now not null.  Plus a value you can identify.  If you see this in your debugger then you know you have never updated the value.

This post has been edited by tlhIn`toq: 29 May 2012 - 03:01 PM

Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: Reading from XMLs

Posted 30 May 2012 - 02:46 AM

How I would do this (quick and dirty):

using System;
using System.Xml;

namespace test
{
	public class Tester
	{
		static void Main()
		{
			XmlDocument doc = new XmlDocument();
			doc.Load("twister.xml");

			// Use XPath to select the value you want.
			XmlNodeList nodes = doc.SelectNodes("/test/register/account/email");
			if (nodes != null && nodes.Count > 0)
				Console.WriteLine("Email is {0}", nodes[0].InnerText);
                        nodes = doc.SelectNodes("//hotel/segment");
			if (nodes != null && nodes.Count > 0)
				Console.WriteLine("Segment is {0}", nodes[0].InnerText);
		}
	}
}


$ mono twister.exe 
Email is test@test.com
Segment is upscale
$

Was This Post Helpful? 0
  • +
  • -

#10 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Reading from XMLs

Posted 30 May 2012 - 05:41 AM

View PosttlhIn`toq, on 29 May 2012 - 06:01 PM, said:

A breakpoint stops execution at the start of the line, before that line is executed.
I would expect Email to be null at the START of line 58 if you didn't initialize it upon creation, because that line has not yet run and therefore no value has been applied yet.

Press F10 to execute this line and move to line 59. You should see that "Email" now has a value.

Its going to be very hard to debug if you don't understand the tools you're using.

Also, if you aren't initializing your variables/objects at the time you create them then you are just begging for these (null reference) problems.

string Email; // Bad
string Email = "unknown"; // Initial known value.  Now not null.  Plus a value you can identify.  If you see this in your debugger then you know you have never updated the value.


For some stupid reason I had those variables set like this:
String email {get; set;}


Haha.

However, It's not really the problem that my variable is not set. The issue is when my code tries to execute:
data["email"].InnerText;

It crashes every time by trying to read the XML and I just don't understand why it can't read it when I'm giving it the path very clearly!

This post has been edited by tlhIn`toq: 30 May 2012 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#11 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Reading from XMLs

Posted 30 May 2012 - 06:21 AM

I have figured out a quick fix for it right now. I will have to clean up my code, soon, now. lol.

Email = data.ChildNodes[0].ChildNodes[0].InnerText;



If you all have any better answers I would love to hear it. Thank you for all your help!
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Reading from XMLs

Posted 30 May 2012 - 06:59 AM

View PostTwister1002, on 30 May 2012 - 06:41 AM, said:

For some stupid reason I had those variables set like this:
String email {get; set;}



There's nothing wrong with properties like this. But you should probably make an initalization method for your class, or do it in the constructor

String email {get; set;}

void ClassConstrutor()
{
   InitVariables();
}

private void InitVariables()
{
    email = "unknown";
    nameFirst = "unknown";
    nameLast = "unknown";
    DoB = new DateTime("01/01/1900 00:00:00");
    // etc
}


Was This Post Helpful? 1
  • +
  • -

#13 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Reading from XMLs

Posted 30 May 2012 - 10:20 AM

View PosttlhIn`toq, on 30 May 2012 - 09:59 AM, said:

View PostTwister1002, on 30 May 2012 - 06:41 AM, said:

For some stupid reason I had those variables set like this:
String email {get; set;}



There's nothing wrong with properties like this. But you should probably make an initalization method for your class, or do it in the constructor

String email {get; set;}

void ClassConstrutor()
{
   InitVariables();
}

private void InitVariables()
{
    email = "unknown";
    nameFirst = "unknown";
    nameLast = "unknown";
    DoB = new DateTime("01/01/1900 00:00:00");
    // etc
}



I was actually putting doing that code in the constructor, which I probably shouldn't have. It's okay. I'm gonna have to go through it and clean it up at some point. Just wanted that done with. Haha.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1