Replacing some text

Linkz0rs

Member
Messages
247
Reaction score
7
Points
18
Could someone help me out? I'm a bit confused...
This is for a chat server application I have made, and it outputs all information into the chatroom.

This is the code
Code:
private void Work()
        {
            this.busy = true;

            try
            {
                WebRequest request = WebRequest.Create("http://api.ipinfodb.com/v2/ip_query.php?key=MYKEY&ip=" + this.query);
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.Default);
                String[] lines = reader.ReadToEnd().Split(new String[] { "\n" }, StringSplitOptions.None);

                TraceObject obj = new TraceObject();
                obj.country = lines[4];
                obj.region = lines[6];
                obj.city = lines[7];
                obj.SetTime(lines[9], lines[10]);

                reader.Close();
                stream.Flush();
                stream.Close();
                response.Close();

                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x000310\x0007Trace Result for: " + this.name));
                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket(" "));

                bool color1 = true;

                if (obj.country.Length > 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Country: " + obj.country));
                    color1 = !color1;
                }

                if (obj.region.Length > 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Region: " + obj.region));
                    color1 = !color1;
                }

                if (obj.city.Length > 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "City: " + obj.city));
                    color1 = !color1;
                }

                if (obj.got_time)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Local Time: " + obj.localtime));
                    color1 = !color1;
                }

                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket(" "));
                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x000310\x0007Trace Complete"));
            }
            catch
            {
                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("Unable to trace a result for " + this.name));
            }

            this.busy = false;
        }
    }

This is for IP tracing in the chatroom.
When an admin in the chatroom sends the trace command, it processes this command.
It utilizes the site
http://api.ipinfodb.com/v2/ip_query.php?key=MYKEY&ip=

It automatically plugs in an IP address.
It returns results with absolutely no problems.
The only thing is...
How do I strip out the tags, immediately after it grabs the information, before processing it into the chatroom?
I want "<CountryName/>", "<RegionName/>", and "<City/>" to be stripped out... So it only shows the information I want...

As of now, this is how it outputs into the chatroom:
Trace Result for: (ip removed, u dont need my ip)

Country: <CountryName>United States</CountryName>
Region: <RegionName>Virginia</RegionName>
City: <City>Norfolk</City>

Trace Complete
 
Last edited:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Why aren't you using the .NET XML classes? Text parsing (as you're doing at the moment) is fragile -- it depends on newlines (which, by the way, don't need to be there and could be removed from the API return without notice) and on the ordering of the information staying consistent throughout the lifetime of the service (and since ordering of sibling elements doesn't have any "meaning" in an XML document, that's subject to change without notice as well). All you'd need to so using the XML classes is to get the values of the CountryName, RegionName and City elements -- any special characters in the values that need to be escaped to entities to create valid XML would automatically be reconverted to plain text as well. It's just easier all around.
 

LostHorizon

Member
Messages
43
Reaction score
2
Points
8
Hi "Linkz0rs",

I do not need anything to be replaced, because it functions just fine as it is and do not want to have to rewrite the entire server.

If you want to KEEP your old codes, I'd like to help you, but unfortunately I'm not too familiar with XML (if that's the script language that you're using). The best that I can do is to show you WHERE you'll need to modify your codes to strip out the unwanted tags as highlighted below:

Code:
if (obj.country.Length > 0)
{
   UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Country: " + [COLOR="red"]obj.country[/COLOR]));
   color1 = !color1;
}

if (obj.region.Length > 0)
{
   UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Region: " + [COLOR="red"]obj.region[/COLOR]));
   color1 = !color1;
}

if (obj.city.Length > 0)
{
   UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "City: " + [COLOR="red"]obj.city[/COLOR]));
   color1 = !color1;
}


********
As for the stripping of the unwanted tags, here are the JavaScript codes for it (I looked it up on the Internet, and it turns out that XML uses the same method too):

Code:
obj.country.replace(/<(|\/)CountryName>/g,"")
obj.region.replace(/<(|\/)RegionName>/g,"")
obj.city.replace(/<(|\/)City>/g,"")

All you have to do is just simply REPLACING the codes ("obj.country", "obj.region", and "obj.city") that I've highlighted in the Code Box #1 with the replacement codes in the Code Box #2 respectively.


********
Hope this helps.
 

Linkz0rs

Member
Messages
247
Reaction score
7
Points
18
Why aren't you using the .NET XML classes? Text parsing (as you're doing at the moment) is fragile -- it depends on newlines (which, by the way, don't need to be there and could be removed from the API return without notice) and on the ordering of the information staying consistent throughout the lifetime of the service (and since ordering of sibling elements doesn't have any "meaning" in an XML document, that's subject to change without notice as well). All you'd need to so using the XML classes is to get the values of the CountryName, RegionName and City elements -- any special characters in the values that need to be escaped to entities to create valid XML would automatically be reconverted to plain text as well. It's just easier all around.

I'm not that familiar with XML, I tried it but I just kept getting errors that I couldn't resolve.


Code:
obj.country.replace(/<(|\/)CountryName>/g,"")
obj.region.replace(/<(|\/)RegionName>/g,"")
obj.city.replace(/<(|\/)City>/g,"")

All you have to do is just simply REPLACING the codes ("obj.country", "obj.region", and "obj.city") that I've highlighted in the Code Box #1 with the replacement codes in the Code Box #2 respectively.

That didn't work, it was throwing all sorts of errors at me.
Some of which...

Unexpected character '\'
invalid expression term'/'
; expected
only assignment, call, increment, decrement, and new object expressions can be used as a statement
the name 'g' does not exist in the current context
no overload for method 'replace' takes 1 arguments


I've tried to modify it by putting " in between the ( and ) and ; on the end of course, but then it throws the error unrecognized escape sequence (for the /'s)

This isnt XML, it's C#... Although, yes, I am reading XML formatted website, I'm simply trying to strip out the tags so it doesnt show in chatroom.

Thanks
 
Last edited:

LostHorizon

Member
Messages
43
Reaction score
2
Points
8
Hi "Linkz0rs",

Sorry about that, I didn't know that you're using C#. I thought that you're using XML, that's why I gave you the codes in JavaScript (which will also work in XML as well) - my BAD. But since we're using C#, try these codes for now (for a QUICK FIX) instead:

Code:
obj.country.Replace("<CountryName>","").Replace("</CountryName>","")
obj.region.Replace("<RegionName>","").Replace("</RegionName>","")
obj.city.Replace("<City>","").Replace("</City>","")

The codes don't look that pretty, but they'll do the job (I HOPE!!!).

Hope this helps.


P.S.: I tried to come up with a more elegant (NICE & NEAT) solution using C# regular expression, but I don't know how to test C# codes (may be you can show me how - normally, I test the codes first to make sure everything works before posting). Anyway, it looks something like this:

Code:
(new Regex([I][COLOR="red"]pattern ???[/COLOR][/I])).Replace(obj.country,"")

All it needs is the string matching pattern which is of "string" type.
 

Linkz0rs

Member
Messages
247
Reaction score
7
Points
18
Hi "Linkz0rs",

Sorry about that, I didn't know that you're using C#. I thought that you're using XML, that's why I gave you the codes in JavaScript (which will also work in XML as well) - my BAD. But since we're using C#, try these codes for now (for a QUICK FIX) instead:

Code:
obj.country.Replace("<CountryName>","").Replace("</CountryName>","")
obj.region.Replace("<RegionName>","").Replace("</RegionName>","")
obj.city.Replace("<City>","").Replace("</City>","")

The codes don't look that pretty, but they'll do the job (I HOPE!!!).

Hope this helps.


P.S.: I tried to come up with a more elegant (NICE & NEAT) solution using C# regular expression, but I don't know how to test C# codes (may be you can show me how - normally, I test the codes first to make sure everything works before posting). Anyway, it looks something like this:

Code:
(new Regex([I][COLOR="red"]pattern ???[/COLOR][/I])).Replace(obj.country,"")

All it needs is the string matching pattern which is of "string" type.

Thank you so much @LostHorizon :biggrin:

Your code didn't originally worked, but it greatly helped!
I modified my code as follows...
Code:
private void Work()
        {
            this.busy = true;

            try
            {
                WebRequest request = WebRequest.Create("http://api.ipinfodb.com/v2/ip_query.php?key=cd35e20043cac21e7bdacc5bc5c9c1f02bcab8281ffd85169a526c5594959104&ip=" + this.query);
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.Default);
                String[] lines = reader.ReadToEnd().Split(new String[] { "\n" }, StringSplitOptions.None);

                TraceObject obj = new TraceObject();
                obj.country = lines[4];
                [B]countryname = [/B]obj.country.Replace("<CountryName>", "").Replace("</CountryName>", "");
                obj.region = lines[6];
                [B]regionname = [/B]obj.region.Replace("<RegionName>", "").Replace("</RegionName>", "");
                obj.city = lines[7];
                [B]cityname = [/B]obj.city.Replace("<City>", "").Replace("</City>", "");
                obj.SetTime(lines[9], lines[10]);

                stream.Flush();
                stream.Close();
                reader.Close();
                response.Close();

                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x000310\x0007Trace Result for: " + this.name));
                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket(" "));

                bool color1 = true;

                if (obj.country.Length > 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Country: " + [B]countryname[/B]));
                    color1 = !color1;
                }

                if (obj.region.Length > 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "Region: " + [B]regionname[/B]));
                    color1 = !color1;
                }

                if (obj.city.Length > 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x00030" + (color1 ? "6" : "3") + "City: " + [B]cityname[/B]));
                    color1 = !color1;
                }

                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket(" "));
                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("\x000310\x0007Trace Complete"));
            }
            catch
            {
                UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("Unable to trace a result for " + this.name));
            }

            this.busy = false;
        }
    }

Again, thank you. You were a very big help. ^_^

It now outputs in the chatroom as expected.
Trace Result for: (ip hidden)

Country: United States
Region: Virginia
City: Norfolk

Trace Complete
 
Last edited:
Top