如何删除c#中的非法字符 [英] how to remove illegeal characters in c#

查看:352
本文介绍了如何删除c#中的非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出了代码

the code is given below

string m_URL = "http://www.ft.com/rss/home/us";
string xmlStr;
using (var wc = new WebClient())
{
    wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
    xmlStr = wc.DownloadString(m_URL);
}
         
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);

var bdata = XDocument.Load(xmlStr);





在上面的代码中bdata路径中的非法字符如何删除非法字符。





路径中的非法字符

在此先感谢...



In the above code bdata the illegal charecters in path how to remove the illegal characters.


Illegal characters in path
Thanks in Advance...

推荐答案

在您的情况下,您从网站获取数据,并将其用作路径到文件。



因此,非法字符是您从网站获取的数据的一部分,超出我们或您的控制范围。

很有可能你提取的数据根本不是一个有效的路径 - 它可能是XML或HTML数据。

所以使用调试器,看看你正在构建的数据,看看实际上是什么数据。然后你就可以开始决定路径中的合法人物是什么了。
In your case, you are fetching the data from a website, and using that as a "path" to a file.

So the illegal characters are part of the data you fetched from the site, and beyond our - or your - control.
The chances are that the data you have fetched is not a valid path at all - it's probably XML or HTML data.
So use the debugger, look at exactly what you are fecting and see what the data actually is. Then you can start deciding what is and isn't a "legal character" in your path.


你好Athul,

XDocument没有任何合适的方法可以将整个xml字符串作为输入并加载该xml。它只有你必须传递来源网址的方法。



请看屏幕截图。



屏幕截图 [ ^ ]
Hi Athul,
XDocument Do not have any suitable method which can take whole xml string as input and load that xml. It have only method in which you have to pass URL of the source.

Please see the screen shot.

Screen Shot[^]


string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
    illegal = illegal.Replace(c.ToString(), "");
}


这篇关于如何删除c#中的非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆