gheddo
gheddo

Reputation: 9

Ip Address Parse Problem in C#

I am reading IPaddress from xml file and I put it into IPaddress.parse() and then use it but it does not work. it says "An invalid IP address was specified." but when I write it manually it works.

why I cant use IP address after reading xml file. I tried to erase "white spaces" , it said samething again.

string ipadd; //take ip address from xml and use
 ...
 IPAddress ipaddre = IPAddress.Parse(ipadd); 
------------------------------
 IPAddress ipaddre = IPAddress.Parse("255.255.255.255")

why these are not giving same result?

Upvotes: 0

Views: 3403

Answers (2)

Dekker500
Dekker500

Reputation: 821

On the assumption that ipadd = "255.255.255.2552", then only two possibilities exist:

1) An exception is being thrown on the parse, and therefore your compare is not happening, or 2) The method you are using to compare the two results is not appropriate.

I strongly suggest you add both statements and place a breakpoint on the next line, then you will see exactly what you are dealing with:

IPAddress ipaddreReal = IPAddress.Parse(ipadd);
IPAddress ipaddreFake = IPAddress.Parse("255.255.255.255");
bool result = ipAddre.Real.Equals(ipaddreFake);

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245419

If ipadd == "255.255.255.255" then there should be nothing stopping that from working.

Obviously, though, ipadd != "255.255.255.255"

I would suggest debugging, setting a breakpoint, and inspecting the value of ipadd when you pass it to the IPAddress.Parse() method.

Upvotes: 3

Related Questions