我在c#中编写了一个小程序,其中我显示了一些指向LinkLabels文件的链接。 url和显示的文本是从xml文件加载的 xml文件如下所示:
<member>
<qenum>2</qenum>
<vindex>[3]</vindex>
<name>Aldehyde-Wikipedia (Graphik)</name>
<filename>Wikipedia\Aldehyde - Wikipedia.htm</filename>
</member>
要处理链接,我使用命令System.Diagnostics.Process.Start(this.path + this.filename); (路径和文件名都是从xml文件中读取的字符串值)
所以一切正常,除非在文件名中是char“ - ”。 好吧,这可能是由charset引起的。但奇怪的是,当我进入xml文件时,从特定文件中选择文件名(这里:维基百科文件),将其粘贴到我的xml文件中并保护它,它工作正常,链接得到处理。 为什么呢?
顺便说一句,我用自建的xml编辑器创建我的xml文件。在那里,值被写成一个字符串。如何设置LinkLabel来处理特殊字符?
编辑[18.4.14]:
这是该类的完整代码,其中处理了lik:
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; }
}
}
}
这是我用来显示项目的4个类别之一:
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);
}
}
}
}
我在win8下这样做。当我点击其中一个维基百科条目时,它会给我一个错误消息,说明找不到该文件(维基百科htm文件)。在win7下,当我想显示链接时(当我点击按钮时显示在面板上),errormessage会直接提示。它说,在第一个“ - ”字符位置的xml文件中有一个字符编码错误。
然后我找到了一种方法来欺骗这个错误。我只是从windows-explorer复制了文件名(htm)并替换了xml文件中的字符串。并且瞧它工作得很好。但为什么呢?
答案 0 :(得分:0)
我刚刚发现了问题所在:在我的字符串值中,“ - ”字符是一个短字符(短字符)但实际文件中有“ - ”字符(em-dash;稍长) 。所以可能的解决方案可能是 1)用字符串值本身或者用em-dash替换en-dash 2)以编程方式替换实际文件中的破折号
我尝试了第一种方法,但它不起作用(在一个简单的if语句中)
是否有可能通过其他拼写搜索字符(例如十六进制值)?