0

I've written a small programm in c# where i display some links to files with LinkLabels. the url and the displayed text are loaded from a xml-file the xml-file looks like this:

<member>
    <qenum>2</qenum>
    <vindex>[3]</vindex>
    <name>Aldehyde-Wikipedia (Graphik)</name>
    <filename>Wikipedia\Aldehyde - Wikipedia.htm</filename>
  </member>

to process the link, i use the command System.Diagnostics.Process.Start(this.path + this.filename); (path and filename are both string values read from the xml-file)

so everything work fine except when in the filename is the char "-". ok this might be caused by the charset. but the odd thing is, when i go into the xml file, select the filename from the specific file (here: the wikipedia file), paste it into my xml file and safe it, it works just fine, the link gets processed. why?

oh by the way, i create my xml file with a selfbuild xml-editor. there, the values are written as a string.

how can i set the LinkLabel to process special characters?

EDIT [18.4.14]:

here is the full code of the class, where the lik is processed:

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Forms = System.Windows.Forms;
using System.Windows.Forms;
using System.Diagnostics;

namespace SourceView
{
class linklabel
{
    private Forms.LinkLabel link;
    private Forms.Label qlabel = new Forms.Label();
    private int x, x2, y;
    private String filename;
    private String type;
    private String path;

    public linklabel(int X, int Y, String type)
    {
        link = new LinkLabel();
        x = 95;
        x2 = 15;
        y = 30;
        this.type = type;
        switch (type)
        {
            case "web":
                this.path = @"web\";
                break;
            case "pdf":
                this.path = @"pdf\";
                break;
            case "pic":
                this.path = @"graphics\";
                break;
        }

        this.link.AutoSize = true;
        this.link.Location = new System.Drawing.Point(x + X, y + Y);
        this.link.Name = "link";
        this.link.Size = new System.Drawing.Size(72, 17);
        this.link.TabIndex = 4;
        this.link.TabStop = true;
        this.link.Text = "link";
        this.link.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.link_LinkClicked);

        this.qlabel.AutoSize = true;
        this.qlabel.Location = new System.Drawing.Point(x2 + X, y + Y);
        this.qlabel.Name = "qlabel";
        this.qlabel.Size = new System.Drawing.Size(50, 17);
        this.qlabel.Text = "vindex";
        this.qlabel.TabIndex = 5;
        this.qlabel.TabStop = true;

        x2 = x2 + X;
        x = x + X;
        y = y + Y;
    }

    private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        Console.WriteLine(this.filename);
        try
        {
            if (this.filename.Contains("-"))
            {
                MessageBox.Show("Es ist ein Fehler in der von Ihnen angeforderten Datei aufgetreten. Bitte überprüfen Sie den Dateinamen auf Sonderzeichen und entfernen Sie diese auch in der entsprechenden Referenzdatei.");
            }
            System.Diagnostics.Process.Start(this.path + this.filename); //this.path + this.filename
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n" + "Bitte korrigieren Sie den Dateipfad in der Datei " + type + "Data.xml" + "\n" + "Fehler im String " + this.path + this.filename);
        }
    }

    public void setTitle(String vindex, String title)
    {
        this.qlabel.Text = vindex;
        this.link.Text = title;
    }

    public void setLink(String filename)
    {
        this.filename = filename;
    }

    public Forms.LinkLabel label
    {
        get { return link; }
    }

    public Forms.Label Qlabel
    {
        get { return qlabel; }
    }
}
}

and this is one of the 4 classes, which i use to display the items:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Forms = System.Windows.Forms;
using System.Windows.Forms;
using System.Xml;
using System.Runtime.InteropServices;
using System.Drawing;

namespace SourceView
{
class WebPnl : IMessageFilter
{

    private MainForm mf;
    private Forms.Panel pnlWeb = new Forms.Panel();
    private Forms.Label lblInfo = new Forms.Label();

    private linklabel[] links;  

    String spacer = "    ";

    public WebPnl(MainForm aufrufer)
    {
        mf = aufrufer;

        //
        //lblInfo
        ///
        this.lblInfo.Location = new System.Drawing.Point(5, 5);
        this.lblInfo.Text = "Querverweis" + spacer + "Titel";
        this.lblInfo.Size = new System.Drawing.Size(150, 25);
        //
        //pnlWeb
        //
        this.pnlWeb.Controls.Add(this.lblInfo);
        this.pnlWeb.BackColor = System.Drawing.Color.Transparent;
        this.pnlWeb.Location = new System.Drawing.Point(890, 75);
        this.pnlWeb.Name = "pnlWeb";
        this.pnlWeb.Size = new System.Drawing.Size(350, 695);
        if(mf.Width >= 1400)
            this.pnlWeb.Size = new System.Drawing.Size(490, 695);
        this.pnlWeb.TabIndex = 4;
        this.pnlWeb.AutoScroll = true;
        this.pnlWeb.Visible = false;
    }

    //mouse wheel support for scrollable controls
    #region mouse wheel support
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(Point pt);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x20a)
        {
            Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
            IntPtr hWnd = WindowFromPoint(pos);
            if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null)
            {
                SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
                return true;
            }
        }
        return false;
    }
    #endregion

    public void Init()
    {
        loadWebPages();
        mf.Controls.Add(pnlWeb);
    }

    public void show()
    {
        this.pnlWeb.Visible = true;
    }

    public void remove()
    {
        this.pnlWeb.Visible = false;
    }

    //helper function for loading
    private string getElement(string field, ref XmlElement element)
    {
        string value = "";
        try
        {
            value = element.GetElementsByTagName(field)[0].InnerText; 
        }
        catch (Exception e)
        {
            //ignore error, return empty
            Console.WriteLine(e.Message);
        }
        return value;
    }

    private void loadWebPages()
    {
        int l = 0;

        try
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("webData.xml");
            XmlNodeList list = doc.GetElementsByTagName("member");

            l = list.Count;
            links = new linklabel[l];

            int i = 0;
            int y = 5;
            foreach (XmlNode node in list)
            {                 
                XmlElement element = (XmlElement)node;

                links[i] = new linklabel(0, y, "web");
                links[i].setTitle(getElement("vindex", ref element), getElement("name", ref element));
                links[i].setLink(getElement("filename", ref element));
                Console.WriteLine(getElement("filename", ref element));
                this.pnlWeb.Controls.Add(links[i].label);
                this.pnlWeb.Controls.Add(links[i].Qlabel);

                Console.WriteLine(i);
                i++;
                y = y + 25;
            }
        }
        catch (Exception es)
        {
            MessageBox.Show(es.Message);
        }
    }

}
}

i'm doing this here under win8. when i click one of the wikipedia-entries, it gives me an errormessage which says, that the file could not be found (the wikipedia htm-file). under win7, the errormessage promts directly when i want to display the links (which are displayed on a panel when i click a button). it says, that there is a character encoding error in the xml-file at the position of the first "-" character.

then i found a way to trick this error. i simply copied the filename (htm) from the windows-explorer and replaced the string in the xml-file. and voilà it works just fine. but why?

  • We need to see how you “use the command System.Diagnostics.Process.Start(this.path + this.filename)”. and what the values of `path` and `filename` are. – Dour High Arch Apr 16 '14 at 22:17
  • i use the command within a onclick event of the linklabel. filename is the value read out of the xml-file. the path value is just because i use this class for 4 different save folders. – Ueda Ichitaka Apr 17 '14 at 18:45

1 Answers1

0

I just found out what the problem is: in my string values, the "-" character is a en-dash (the short one) but the actual file has the "–" character (em-dash; slightly longer). so a possible solution might be 1) to replace the en-dash with an em-dash in the string value itself or 2) to replace the dash in the actual file programmatically

i tried the first approach but it didn't work (in an simple if-statement)

is there a possibility to search for a char by an other spelling (e.g. hex-value)?

  • If you have more information, please [edit](http://stackoverflow.com/posts/23110276/edit) your question, do not post it as an answer. – Dour High Arch Apr 18 '14 at 21:30