Showing posts with label parsing. Show all posts
Showing posts with label parsing. Show all posts

Monday, March 26, 2012

Stringbuilder Parsing Text: Need to remove square character

Hi everyone,

I am attempting to parse data from a webpage and want to remove any unneccesary junk from the string. I start by removing any Tags from the text I have retrieved from the web page. Next, I split the text into an array. However, I end up with a string that contains a bunch of characters that look like ?, sometimes there are many in a row: ???? in each string. I would like to remove these characters.

These characters are the only things left separating me from the data I want, but I am not sure how to remove them... Does anyone have an idea? It is interesting that when I display my string in a textbox, or using a label, the squares are not visible (I think they display as spaces) but when I debug I see them.

Thanks,

zoop

Those chars probably are line feeds, tabs or some other control chars.

Try:

Dim junk asString ="bla bla bla....."

junk=junk.replace(ControlChars.Lf,"")

junk=junk.replace(ControlChars.NewLine,"")

junk=junk.replace(ControlChars.Tab,"")


etc, etc...


If I'm not mistaken you are seeing the hidden space place holder. You can use regular expressions to remove them. Try something like below to remove them.

using System.Text.RegularExpressions;

private string StripSpaces(string inputString)
{
return Regex.Replace(inputString, @."[^\s$]","");
}


Thanks for the input fellas. I have tried the suggestions above along with some variations and came up short. Replacing the ControlChars does not seem to make a difference on the format of the string. Running the stripSpaces function removes all of the data I am interested in and returns only the chars I am attempting to remove (blanks, or squares). Perhaps I lost something when I translated it into VB...

Anyway, since there is not much code to go over I thought perhaps it would help if I posted what I have here. I am going to omit the section where I replace any ControlChars for brevity, as it does not seem to make a difference.

1Private Sub btnSubmit_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btnSubmit.Click2'// Store Page Source in DOC3Dim reqAs WebRequest = WebRequest.Create("http://world5.knightfight.co.uk/index.php?ac=highscore&vid=0")4Dim respAs WebResponse = req.GetResponse56Dim sAs Stream = resp.GetResponseStream7Dim srAs StreamReader =New StreamReader(s, Encoding.ASCII)8Dim docAs String = sr.ReadToEnd910'// Format text in DOC using StringBuilder Class11Dim sbAs StringBuilder =New StringBuilder(doc)12Dim endIndexAs Integer = sb.ToString.IndexOf("showuserid=")13Dim newString()As String14Dim strTestAs String1516'// Remove Unwanted Text17strTest = sb.Remove(0, endIndex).ToString1819'// Remove HTML Tags and Spaces20strTest = StripTags(strTest)21strTest = stripSpaces(strTest)2223'// TEMP - Display Preview in Multiline Textbox24txtbWebData.Text = strTest2526'// Split String into Array27newString = Split(strTest," ")28End Sub2930Public Function StripTags(ByVal HTMLAs String)As String31' Removes tags from passed HTML32Dim objRegExAs _33System.Text.RegularExpressions.Regex34Return objRegEx.Replace(HTML,"<[^>]*>","*")35End Function3637Public Function stripSpaces(ByVal inputStringAs String)As String38Dim objRegExAs _39System.Text.RegularExpressions.Regex40Return objRegEx.Replace(inputString,"[^\s$]","")41End Function42

I thought it would be an interesting exercise to try and capture some data from a highscores list on a website. The URL is in the code above, http://world5.knightfight.co.uk/index.php?ac=highscore&vid=0,so you could reproduce my results if you like. The end goal is to capture the data in the highscores list and format it in a way that makes sense. If you think there is a better approach please let me know :).

Thanks,

zoop


Sorry about that. Replace Return objRegEx.Replace(inputString, "[^\s$]", "") with Return objRegEx.Replace(inputString, "[^\S$]", ""). The \s matches any white space character and the \S matches any non-white space character.

Strings and parsing and errors with split

Hi

I am fairly new to .NET. However, I am very experienced with ASP. Here is the error I am getting
Value of type '1-dimensional array of String' cannot be converted to 'String'.
I am trying the following:
Dim tot As String
tot=cmd3.executescalar()
tot = split(tot,",")
response.write (tot)
Does anyone have any ideas of how I can get the values "1,2,3" from the database (as a string), parse those into an array, and then have the database look up each value and bid the results to the page. I have the datagrid in place. At the moment, my code works with 1 variable from the database (like 12), but not multiples like "1,2,3". The numbers could be anything too - "12,17" or "1,4,5,7,33,46"
Any ideas are appreciated - I am stuck as of now.
Thanks
HBHey,

You are on the right track, but you need to declare tot as an array, as such:

Dim nbrs As String = cmd3.ExecuteScalar()
if (nbrs <> String.Empty) Then
Dim tot() As String = nbrs.split(","c)
'Loops through the entries in the array
for each strID As String in tot
Response.Write(strID & "<br>")
next
end if

Brian
Wow - thanks for the quick response - I will try it out today.
HB
There is still and error:
<code>
Dim tot() As String = nbrs.split(","c)
<code
The error is - Variable 'tot' hides a variable in an enclosing block.
Is this "c" suppose to be there? I also tried nbrs.split(tot,",") and it gave back the same error.
I think I am getting close. I also need to use each of the numbers from the array in a SQL statement to get information from the DB and bind it. So, there will be one column with 3 results from the array containing 3 elements.

Thanks
HB
c means convert "," to a char. In C#, you denote chars with ', which obviously you can't do in VB.NET.

I think it means you are using tot outside of the if statement block, which the object wouldn't exist. Could you post your entire code so I can verify?

Brian
Sure - here is the code - it's messy at the moment. What it is suppose to do is select a product from the main page and then publish the 3 chemicals which are used for the product. So, I am using a one to many relationship. The data is stored in the database as 1,2,3 and I must take the 1,2,3 and get product 1, then product 2, then product 3. I also need to get away from the scalar - it should be Reader after closer inspection.
<code>
<%@. Page Language="VB" Debug="True" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Collections" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<%@. Import Namespace="System.Configuration.ConfigurationSettings" %>
<%@. Register TagPrefix="header" TagName="top" src="http://pics.10026.com/?src=header.ascx" %>
<%@. Register TagPrefix="footer" TagName="bottom" src="http://pics.10026.com/?src=footer.ascx" %>
<html>
<head>
<title></title>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim Conn As SQLConnection
Dim Rdr,Rdr2 As SQLDataReader
Dim Q1,Q2,Q3,total,number,Names,stringSQL2,Word As string
Dim x As integer

Q2 = Request.QueryString("product")
Dim strConn As string = AppSettings("connect")

Dim stringSQL As string = "Select " & Q2 & " from menu_green"

Dim SQL as string="Select names1,names2,names3 from menu_green where C1='" & Q2 & "'"
SQL = SQL & "OR C2='" & Q2 & "'"
SQL = SQL & "OR C3='" & Q2 & "'"
Dim Conn2 as New SQLConnection(strConn)
Dim Cmd3 as New SQLCommand(SQL,Conn2)
Conn2.Open()
Dim tot As String
'litHowMany.text=cmd3.executescalar()
tot=cmd3.executescalar()
'Response.write ("" & tot)
'Conn2.Close()

Dim nbrs As String = cmd3.ExecuteScalar()
if (nbrs <> String.Empty) Then
Dim tot() As String = nbrs.split(","c)
'Loops through the entries in the array
for each strID As String in tot
Response.Write(strID & "<br>")
next
end if

stringSQL2 = "Select id,names from products where id='" & tot & "'"
Conn = New SQLConnection(strConn)
Dim Cmd2 As New SQLCommand(stringSQL2,Conn)
Conn.Open

'Dim myname As string
'myname = cmd2.executescalar()
'response.write ("" & myname & "<br>")

Rdr2 = Cmd2.ExecuteReader()
myproducts.datasource = Rdr2
myproducts.databind()
Conn.close

Conn = New SQLConnection(strConn)

Dim Cmd As New SQLCommand(stringSQL,Conn)
'Dim Cmd2 As New SQLCommand(stringSQL2,Conn)

Conn.Open
Rdr = Cmd.ExecuteReader()
mylist.datasource = Rdr
mylist.databind()
Conn.close

End Sub
</script>
<code
Thanks for any input

HB
One thing: You declared tot twice, once as a variable, once as an array.

Tuesday, March 13, 2012

strugling with Atributed XML parsing.

I have XML in following format
<ORDER>
<HEADER> ... </HEADER>
<LINE> ... <LINE>
<LINE> ... <LINE>
<LINE> ... <LINE>
</ORDER
Can someone suggest how will i parseout such document using XmlSerializer
and attributes.
There can be different amount of lines in different documents.

I have

class clsOrder

{

clsHeader _header;

clsLine [] Lines;

}

How do i specify that <line> goes into Lines array?

Thanks.

George.Hi George,

Thanks for posting in the community!
From your description, you need some suggestions or information on how to
specify the proper XmlAttributes in certian classes so as to use
XmlSerializer to output them and the certain custom class you made contains
an array member of another custom class, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I suggest that you use the "XmlArray" and
XmlArrayItem" attribute to implement the control of the array member within
a certain class's serialization. For example, we can provide the class's
code as below:

public class clsLine
{
public clsLine()
{}

public string Name = "name";
public string Description = "description";
}

public class clsOrder
{
public clsOrder()
{}

[XmlElement("MyHeader")]
public string Header = "header";

[XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
[XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
public clsLine[] Lines = null;

}

Notice the two attributes specified in the clsOrder class.
1. the "XmlArray" has specified what name to use for the array member when
the class's instance is serialized, here I specify it as "MyLines", and the
"XmlArrayItem" can be used to specify the array member's item's serialize
options, and when i serialzie a certain clsOrder instance via the following
code:
----------------
XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));

clsOrder odr = new clsOrder();
odr.Header = "Test Order";
clsLine[] lines = new clsLine[5];

for(int i=0;i<lines.Length;i++)
{
clsLine line = new clsLine();
line.Name = "Name" + i.ToString();
line.Description = "Description" + i.ToString();
lines[i] = line;
}

odr.Lines = lines;

TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,odr);
writer.Close();
------------
The output.xml is like:
----------output.xml--------
<?xml version="1.0" encoding="utf-8"?>
<clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine>
<Name>Name0</Name>
<Description>Description0</Description>
</MyLine>
<MyLine>
<Name>Name1</Name>
<Description>Description1</Description>
</MyLine>
<MyLine>
<Name>Name2</Name>
<Description>Description2</Description>
</MyLine>
<MyLine>
<Name>Name3</Name>
<Description>Description3</Description>
</MyLine>
<MyLine>
<Name>Name4</Name>
<Description>Description4</Description>
</MyLine>
</MyLines>
</clsOrder>
----------------

In addition, here is some tech references in MSDN on the how to use and
control the XmlSerializer, I believe they'll be helpful to you:

#Introducing XML Serialization
http://msdn.microsoft.com/library/e...roducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/e...xampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/e...trollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/e...ributesthatcont
rolserialization.asp?frame=true

Please check out my preceding suggestions, if you have any questions on
them, please feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
That is the problem.
Your sample creates following XML
<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
</MyLines>
</clsOrder
Unfortunately i need following structure.

<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
</clsOrder
Notice there is no <MyLines> tag

I do agree that your structure is more natural and i would not even ask my
question if that was a case. But i stuck with aftermarket standart which was
created by some guys with jumbo heads (joke) and need to be able to parse
that XML

Thanks.
George.

"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:DaO7nzf6DHA.3736@.cpmsftngxa07.phx.gbl...
> Hi George,
>
> Thanks for posting in the community!
> From your description, you need some suggestions or information on how to
> specify the proper XmlAttributes in certian classes so as to use
> XmlSerializer to output them and the certain custom class you made
contains
> an array member of another custom class, yes?
> If there is anything I misunderstood, please feel free to let me know.
> As for this question, I suggest that you use the "XmlArray" and
> XmlArrayItem" attribute to implement the control of the array member
within
> a certain class's serialization. For example, we can provide the class's
> code as below:
>
> public class clsLine
> {
> public clsLine()
> {}
> public string Name = "name";
> public string Description = "description";
> }
> public class clsOrder
> {
> public clsOrder()
> {}
> [XmlElement("MyHeader")]
> public string Header = "header";
> [XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
> [XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
> public clsLine[] Lines = null;
> }
> Notice the two attributes specified in the clsOrder class.
> 1. the "XmlArray" has specified what name to use for the array member when
> the class's instance is serialized, here I specify it as "MyLines", and
the
> "XmlArrayItem" can be used to specify the array member's item's serialize
> options, and when i serialzie a certain clsOrder instance via the
following
> code:
> ----------------
> XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));
> clsOrder odr = new clsOrder();
> odr.Header = "Test Order";
> clsLine[] lines = new clsLine[5];
> for(int i=0;i<lines.Length;i++)
> {
> clsLine line = new clsLine();
> line.Name = "Name" + i.ToString();
> line.Description = "Description" + i.ToString();
> lines[i] = line;
> }
> odr.Lines = lines;
> TextWriter writer = new StreamWriter("output.xml");
> serializer.Serialize(writer,odr);
> writer.Close();
> ------------
> The output.xml is like:
> ----------output.xml--------
> <?xml version="1.0" encoding="utf-8"?>
> <clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> <MyHeader>Test Order</MyHeader>
> <MyLines>
> <MyLine>
> <Name>Name0</Name>
> <Description>Description0</Description>
> </MyLine>
> <MyLine>
> <Name>Name1</Name>
> <Description>Description1</Description>
> </MyLine>
> <MyLine>
> <Name>Name2</Name>
> <Description>Description2</Description>
> </MyLine>
> <MyLine>
> <Name>Name3</Name>
> <Description>Description3</Description>
> </MyLine>
> <MyLine>
> <Name>Name4</Name>
> <Description>Description4</Description>
> </MyLine>
> </MyLines>
> </clsOrder>
> ----------------
> In addition, here is some tech references in MSDN on the how to use and
> control the XmlSerializer, I believe they'll be helpful to you:
> #Introducing XML Serialization
http://msdn.microsoft.com/library/e...roducingxmlseri
> alization.asp?frame=true
> #Examples of XML Serialization
http://msdn.microsoft.com/library/e...xampleofxmlseri
> alizationwithxmlserializer.asp?frame=true
> #Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/e...trollingseriali
> zationbyxmlserializerwithattributes.asp?frame=true
> #Attributes That Control XML Serialization
http://msdn.microsoft.com/library/e...ributesthatcont
> rolserialization.asp?frame=true
> Please check out my preceding suggestions, if you have any questions on
> them, please feel free to let me know.
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Hi George,

Thank you for the response. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)
Hi George,

We are still researching this issue. We will post more information as soon
as we can.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computers security.

This posting is provided "AS IS", with no warranties, and confers no rights.

-------
> X-Tomcat-ID: 199496731
> References: <OxJQSeZ6DHA.2496@.TK2MSFTNGP09.phx.gbl>
<DaO7nzf6DHA.3736@.cpmsftngxa07.phx.gbl>
<u4mVwal6DHA.1428@.TK2MSFTNGP12.phx.gbl>
> MIME-Version: 1.0
> Content-Type: text/plain
> Content-Transfer-Encoding: 7bit
> From: v-schang@.online.microsoft.com (Steven Cheng[MSFT])
> Organization: Microsoft
> Date: Wed, 04 Feb 2004 11:54:58 GMT
> Subject: Re: strugling with Atributed XML parsing.
> X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> Message-ID: <wwyD$Wx6DHA.1988@.cpmsftngxa07.phx.gbl>
> Newsgroups: microsoft.public.dotnet.framework.aspnet
> Lines: 9
> Path: cpmsftngxa07.phx.gbl
> Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:207429
> NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
> Hi George,
> Thank you for the response. Regarding on the issue, I am
> finding proper resource to assist you and we will update as soon as
posible.
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
> with no warranties, and confers no rights.)
I used the Xml Schema tool(xsd.exe) to automatically generate the schema
and serialize mapping class. To my surprise, the tool generated class
worked. Here is the xml format I used as example to generate class:
<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLine> dsfsdffd</MyLine>
<MyLine>dsfsdf</MyLine>
<MyLine> fdfdsf</MyLine>
</clsOrder
Then, I used xsd.exe to generate the schema and class ,here is the
generated class:

[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class clsOrder {

[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string MyHeader;

[System.Xml.Serialization.XmlElementAttribute("MyLine",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public clsOrderMyLine[] MyLine;
}

public class clsOrderMyLine {

[System.Xml.Serialization.XmlTextAttribute()]
public string Value;
}

[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class NewDataSet {

[System.Xml.Serialization.XmlElementAttribute("clsOrder")]
public clsOrder[] Items;
}

It could serialize and deserialize with the xml format the customer exactly
want. Hope this help!

-Thank you
Madhu
Microsoft Developer Engineer

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
closing this thread