【问题标题】:XML schema validation via C#通过 C# 进行 XML 模式验证
【发布时间】:2012-04-15 03:41:00
【问题描述】:

问题: 我正在尝试使用 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 version="1.0" encoding="utf-8"?>
<xsd:schema targetNamespace="http://www.tempuri.com/Courses3.xsd"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns="http://www.tempuri.com/Courses3.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
  <!--definition of simple elements-->
  <xsd:element name="Cap" type="xsd:integer"/>
  <xsd:element name="Room" type="xsd:string"/>
  <xsd:element name="Phone" type="xsd:integer"/>
  <xsd:element name="First" type ="xsd:string"/>
  <xsd:element name="Last" type ="xsd:string"/>
  <xsd:element name="CourseName" type ="xsd:string"/>

  <!--definition of attributes-->
  <xsd:attribute name="Grad" type="xsd:string"/>
  <xsd:attribute name="Undergrad" type="xsd:string"/>
  <xsd:attribute name="Office" type="xsd:string"/>


  <!--definition of complext elements-->

  <!--Courses-->
  <xsd:element name="Courses">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Course" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <!--Course-->
  <xsd:element name="Course">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Code" minOccurs="1" maxOccurs="1"/>
        <xsd:element ref="CourseName" minOccurs="1" maxOccurs="1"/>
        <xsd:element ref="Instructor" minOccurs="1" maxOccurs="1"/>
        <xsd:element ref="Room" minOccurs="0" maxOccurs="1"/>
        <xsd:element ref="Cap" minOccurs="1" maxOccurs="1"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <!--Code-->
  <xsd:element name="Code">
    <xsd:complexType>
      <xsd:attribute ref="Grad" use ="optional"/>
      <xsd:attribute ref="Undergrad" use ="optional"/>
    </xsd:complexType>
  </xsd:element>

  <!--Instructor-->
  <xsd:element name="Instructor">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Name" minOccurs="1" maxOccurs="1"/>
        <xsd:element ref="Contact" minOccurs="0" maxOccurs="1"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <!--Name-->
  <xsd:element name="Name">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="First" minOccurs="1" maxOccurs="1"/>
        <xsd:element ref="Last" minOccurs="1" maxOccurs="1"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <!--Contact-->
  <xsd:element name="Contact">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Phone" minOccurs="0" maxOccurs="1"/>
      </xsd:sequence>
      <xsd:attribute ref="Office" use ="optional"/>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>

我的 XML 实例:

<?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>

我的 C# 验证方法:

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";
    }

我一直在四处寻找,试图弄清楚我忽略了什么。我在此验证中做错了什么?

【问题讨论】:

    标签: c# xml xsd xml-parsing


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。我添加了命名空间。但是,我仍然收到相同的消息。 -P.S 我为这么多内容道歉,我尽量限制它。
    • @Kiwi 我只有几分钟的时间运行了你的代码,它与我添加的建议(几乎)一起工作。您提供的 XSD 未定义 Undergrad 和 Office 属性,否则您提到的所有其他错误都不再存在
    • 好的,我又做了一次。这次效果很好。我将这些 xml 文件存储在互联网上。也许我的缓存有剩余的东西?不确定。谢谢你。 -- 关于为什么我的属性没有被识别的任何提示?看起来我正在我的简单元素定义下方定义我的 XSD 中的属性...
    • 是的,我肯定在某个地方遇到了剩余内存问题,每次更新文件时都会让我感到困惑。我将我的 XML 实例更改为包含命名空间声明“cn:”,并在我的 Undergrad 和 Office 属性前面加上它。这解决了这个问题......但只有在清除我的缓存并重建我的项目之后。那令人沮丧。感谢您的所有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 2015-01-01
    • 2021-08-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多