Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SecurityElement.IsValidText在“0”时返回true&&引用。。。为什么?_C#_Asp.net_Xml - Fatal编程技术网

C# SecurityElement.IsValidText在“0”时返回true&&引用。。。为什么?

C# SecurityElement.IsValidText在“0”时返回true&&引用。。。为什么?,c#,asp.net,xml,C#,Asp.net,Xml,我有一个最终保存在xml节点中的文本框。我正在使用SecurityElement.Escape(string2Escape)在保存xml之前对无效字符进行转义 问题:我尝试使用IsValidText测试是否需要运行escape方法,但它将“”和“&”返回为有效,但当您保存xml时,系统会发出条形码,因为它们实际上无效。似乎只在“”上返回false 简单的解决方案是取消支票,但我的问题是为什么会出现这种情况 以下是我的失败代码: private string EscapeXML(string no

我有一个最终保存在xml节点中的文本框。我正在使用SecurityElement.Escape(string2Escape)在保存xml之前对无效字符进行转义

问题:我尝试使用IsValidText测试是否需要运行escape方法,但它将“”和“&”返回为有效,但当您保存xml时,系统会发出条形码,因为它们实际上无效。似乎只在“”上返回false

简单的解决方案是取消支票,但我的问题是为什么会出现这种情况

以下是我的失败代码:

private string EscapeXML(string nodeText)
{
    if (!SecurityElement.IsValidText(nodeText))
    {
        return SecurityElement.Escape(nodeText);
    }
    return nodeText;
}

这是我从反射器得到的。


这可以解释为什么它的行为方式。我在SecurityElement中没有看到任何方法能够满足您的要求,但它非常简单,可以自己实现一个,可能是作为一个扩展方法。

SecurityElement构造函数显然已经在自己进行一些转义(包括“&”字符),因此,IsValidText似乎只检查构造函数尚未处理的字符。 因此,除非您使用SecurityElement构建整个xml,否则使用SecurityElement的IsValidText/Escape组合看起来并不安全

我将尝试用一个例子更好地解释:

using System;
using System.Diagnostics;
using System.Security;

class MainClass
{
    public static void Main (string[] args)
    {
        // the SecurityElement constructor escapes the & all by itself 
        var xmlRoot =
            new SecurityElement("test","test &");

        // the & is escaped without SecurityElement.Escape 
        Console.WriteLine (xmlRoot.ToString());

        // this would throw an exception (the SecurityElement constructor
        // apparently can't escape < or >'s
        // var xmlRoot2 =
        //    new SecurityElement("test",@"test & > """);

        // so this text needs to be escaped before construction 
        var xmlRoot3 =
            new SecurityElement("test",EscapeXML(@"test & > """));
        Console.WriteLine (xmlRoot3.ToString());

    }

    private static string EscapeXML(string nodeText)
    {
        return (SecurityElement.IsValidText(nodeText))?
            nodeText :
            SecurityElement.Escape(nodeText);
    }
}
使用系统;
使用系统诊断;
使用系统安全;
类主类
{
公共静态void Main(字符串[]args)
{
//SecurityElement构造函数自行转义&all
var xmlRoot=
新的安全元素(“测试”、“测试和”);
//&在没有SecurityElement的情况下转义。转义
Console.WriteLine(xmlRoot.ToString());
//这将引发异常(SecurityElement构造函数)
//显然无法逃脱
//var xmlRoot2=
//新的SecurityElement(“test”,“test&>”);
//因此,在构建之前,需要对该文本进行转义
var xmlRoot3=
新的SecurityElement(“test”,EscapeXML(@“test&>”);
Console.WriteLine(xmlRoot3.ToString());
}
私有静态字符串EscapeXML(字符串nodeText)
{
返回(SecurityElement.IsValidText(nodeText))?
节点文本:
SecurityElement.Escape(nodeText);
}
}