Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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# 在C中验证XML时,不会拒绝不正确的XML格式#_C#_Xml - Fatal编程技术网

C# 在C中验证XML时,不会拒绝不正确的XML格式#

C# 在C中验证XML时,不会拒绝不正确的XML格式#,c#,xml,C#,Xml,我正在验证字符串Weather它是否具有有效的XML格式?虽然我添加了不正确的XML格式 接受需要拒绝的不正确格式 string Parameters="ABC>" string parameters="ABC" 需要接受的coorrect格式被拒绝 string parameters=<Paramnumber AAA="120901" /> 字符串参数= 拒绝 我的代码是: public bool IsValidXML(strin

我正在验证字符串Weather它是否具有有效的XML格式?虽然我添加了不正确的XML格式

接受需要拒绝的不正确格式

      string Parameters="ABC>"

      string parameters="ABC" 
需要接受的coorrect格式被拒绝

      string parameters=<Paramnumber AAA="120901" />
字符串参数=
拒绝

我的代码是:

public bool IsValidXML(string value)
{
    try
    {
        // Check we actually have a value
        if (string.IsNullOrEmpty(value) == false)
        {
            // Try to load the value into a document
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml("<root>" + Parameters+ "</root>");

            // If we managed with no exception then this is valid XML!
            return true;
        }
        else
        {
            // A blank value is not valid xml
            return false;
        }
    }
    catch (System.Xml.XmlException)
    {
        return false;
    }
}
public bool IsValidXML(字符串值)
{
尝试
{
//检查我们是否有一个值
if(string.IsNullOrEmpty(value)==false)
{
//尝试将值加载到文档中
XmlDocument xmlDoc=新的XmlDocument();
LoadXml(“+参数+”);
//如果我们无一例外地管理,那么这就是有效的XML!
返回true;
}
其他的
{
//空值不是有效的xml
返回false;
}
}
catch(System.Xml.XmlException)
{
返回false;
}
}
请让我知道如何正确处理这些问题

问候,

Channa

我认为第三个的正确语法应该是

      string parameters = "<Paramnumber AAA=\"120901\" />";
字符串参数=”;
但是我想你在寻找别的东西。

ABC
都是有效的XML文档。。。不清楚你为什么期望他们被拒绝。但对于第三个示例,这在C#中不是有效的字符串文字,因此它甚至无法编译。