Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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模式验证#_C#_Xml_Xsd_Xml Parsing - Fatal编程技术网

C# 基于C语言的XML模式验证#

C# 基于C语言的XML模式验证#,c#,xml,xsd,xml-parsing,C#,Xml,Xsd,Xml Parsing,问题:我正在尝试使用C#根据XML实例文件验证XML架构文件。然而,我不断收到这些信息: Could not find schema information for the element 'Courses'. Could not find schema information for the element 'Course'. Could not find schema information for the element 'Code'. Could not find schema info

问题:我正在尝试使用C#根据XML实例文件验证XML架构文件。然而,我不断收到这些信息:

Could not find schema information for the element 'Courses'.
Could not find schema information for the element 'Course'.
Could not find schema information for the element 'Code'.
Could not find schema information for the attribute 'Undergrad'.
Could not find schema information for the element 'CourseName'.
Could not find schema information for the element 'Instructor'.
Could not find schema information for the element 'Name'.
Could not find schema information for the element 'First'.
Could not find schema information for the element 'Last'.
Could not find schema information for the element 'Contact'.
Could not find schema information for the attribute 'Office'.
Could not find schema information for the element 'Phone'.
Could not find schema information for the element 'Room'.
Could not find schema information for the element 'Cap'.
我的模式文件(tempuri.com被替换为我实际文件中的实际位置)


我一直在到处寻找,试图找出我忽略了什么。这个验证有什么不对吗

快速浏览一下,因为我没有时间深入研究所有内容,看起来您的XML文件没有定义名称空间,但您的XSD定义了名称空间。这可能是一个开始寻找的地方。在XML文件的根元素中,需要指定名称空间

<Courses xmlns="http://www.tempuri.com/Courses3.xsd">


谢谢。我添加了名称空间。然而,我仍然有同样的信息-补充:我为这么多的内容道歉,我试图尽可能地限制它。@Kiwi我只花了几分钟时间运行了你的代码,它与我添加的建议一起工作(几乎)。您提供的XSD没有定义本科和Office属性,但是您提到的所有其他错误都不再存在了。好吧,我只是重复了一遍。这次效果不错。我将这些xml文件存储在internet上。也许我的储藏室里有剩余的东西?不确定。谢谢有没有关于我的属性没有被识别的提示?看起来我是在简单元素定义的正下方定义XSD中的属性……是的,每次更新文件时,我肯定会遇到内存剩余的问题。我将XML实例更改为包含一个名称空间声明“cn:”,并将其作为我的本科和Office属性的前缀。这解决了问题…但只有在清除缓存并重建项目之后。这令人沮丧。谢谢你的帮助!
<?xml version="1.0" encoding="utf-8"?>
<Courses>
  <Course>
    <Code Undergrad ="CSEXXX"/>
    <CourseName>
      Programming
    </CourseName>
    <Instructor>
      <Name>
        <First>
          Jim
        </First>
        <Last>
          Bob
        </Last>
      </Name>
      <Contact Office ="MLG562">
        <Phone>
          5555555555
        </Phone>
      </Contact>
    </Instructor>
    <Room>
      TLK130
    </Room>
    <Cap>
      70
    </Cap>
  </Course>
public string CoursesVerification(string pXMLurl, string pXSDurl)
    {
        XmlValidatingReader vr = null;
        try
        {
            XmlTextReader nvr = new XmlTextReader(pXMLurl); //get xml file
            nvr.WhitespaceHandling = WhitespaceHandling.None;
            vr = new XmlValidatingReader(nvr); //wrap nvr in vr
            vr.Schemas.Add(GetTargetNamespace(pXSDurl), pXSDurl);
            vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            while (vr.Read());
            return _VerifyString;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            if (vr != null) vr.Close();
        }
    }

    static string GetTargetNamespace(string src)
    {
        XmlTextReader nvr = null;
        try
        {

            nvr = new XmlTextReader(src);
            nvr.WhitespaceHandling = WhitespaceHandling.None;
            while (nvr.Read())
            {
                if (nvr.NodeType == XmlNodeType.Element && nvr.LocalName == "schema")
                {
                    while (nvr.MoveToNextAttribute())
                    {
                        if (nvr.Name == "targetNamespace") return nvr.Value;
                    }
                }
            }
            return "";
        }
        finally
        {
            if (nvr != null) nvr.Close();
        }
    }

    static void ValidationCallBack(object sender, ValidationEventArgs e)
    {
        if (String.Compare(_VerifyString, "No Error") == 0) _VerifyString = e.Message + "\n";
        else _VerifyString += e.Message + "\n";
    }
<Courses xmlns="http://www.tempuri.com/Courses3.xsd">