Michał Gałkowski
Michał Gałkowski

Reputation: 21

C# split with StringReader

i have little problem with my method . So for first i load a html web from my hardware. Its little tinny but : Here is the code from web:

<!DOCTYPE html>
<html>
<head>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <p>001;20151006;0000;1800;1000;999;1;</p>
    <p>001;20151006;0100;1300;990;999;1;</p>
    <p>001;20151006;0200;1100;1000;999;1;</p>
    <p>001;20151006;0300;1500;1100;999;1;</p>
    <p>001;20151006;0400;2200;500;999;1;</p>
    <p>001;20151006;0500;1900;100;999;1;</p>
    <p>001;20151006;0600;0700;990;999;1;</p>
    <p>001;20151006;0700;0300;998;999;1;</p>

</body>
</html>

I just need to take body and load second and 3th row what is yyyyMMdd HH:mm. So here is my code to to this:

 char[] pommidChar = { ';' };
WebRequest request = WebRequest.Create(
              "http://localhost:49443/Wyniki.html");

        request.Credentials = CredentialCache.DefaultCredentials;

        WebResponse response = request.GetResponse();
       Console.WriteLine(((HttpWebResponse)response).StatusDescription);

        Stream dataStream = response.GetResponseStream();

        StreamReader reader = new StreamReader(dataStream);

        string responseFromServer = reader.ReadToEnd();

        //  Console.WriteLine(responseFromServer);

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(responseFromServer);
        // Console.WriteLine(doc);



        string formatString = "yyyyMMddHHmm";//201510060000;
        reader.Close();//   = "201510060000"
        response.Close();
        string[] slowa;

        string tekst ;
        foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//body"))
        {

            string temp = "";
            Console.WriteLine(node.InnerText);
            id = node.InnerText;
            tekst = node.InnerText;
            //Console.WriteLine("Node"+node.ToString());
            slowa = id.Split(pommidChar);
            temp = slowa[1] + slowa[2];
            //Console.WriteLine(slowa[30]);
            Console.WriteLine("string pobrany ze strony"+temp);
            DateTime dt = DateTime.ParseExact(temp, formatString, CultureInfo.InvariantCulture,
                                    DateTimeStyles.None);

            Console.WriteLine("Ostatnia dana z parse:" + dataParse1.ToString("yyyy-MM-dd HH:mm"));
            Console.WriteLine("Aktualna pobrana z urzadzenia:" + dt.ToString("yyyy-MM-dd HH:mm"));

} Now in tekst i have string from //body so i want read line by line and copmare date :

using (StringReader readerr = new StringReader(tekst))
        {
            string[] splitline;
            string line;

            while ((line = readerr.ReadLine()) != null)
            {

               Console.WriteLine(line);

               splitline = line.Split(pommidChar);

              //  Console.WriteLine(splitline[0]);
                Console.WriteLine(splitline[1]);

            }
            readerr.Close();
        }

But , here is the clue .I have only 1 string in "splitstring" its 001 . So someone can describe me where are others string from line? This work slowa = id.Split(pommidChar); work and split exacly what i want but i can`t read line by line then .

I pass this question becuse is done. Now i have one more stupid question for someone. My Dr from university change soruce code from web. Now all variables (html code) are set in 1 row licke this

<body>
    <pre><br>001;20151006;000034;1800;1000;999;1<br>001;20151006;000035;1800;1000;999;1;<br>001;20151006;000036;1800;1000;999;1;</pre>
</body>

So , how i can take string from BR tag . My solution up is take them all in one row . I was thinking that for loop with lenght string is good . I was wrong becuse i don`t know how much string i must get. Remember that 1 "quete" is start with "001" and end with "1". I try to get node as

HtmlNode node in doc.DocumentNode.SelectNodes("//body/pre/br")

but this don`t work.

Upvotes: 1

Views: 757

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

You need to select P tags... I'd expect something like SelectNodes("//p") or SelectNodes("//body/p") instead of selecting single body element (which will as inner text contain concatenation of all strings inside P tags).

Upvotes: 1

Related Questions