3

我正在尝试针对 XSD 架构验证 XML 文件,并且遇到 XmlException 消息

根级别的数据无效。第 1 行,位置 1。

基于对以前类似帖子的爬网,我做了以下工作:

  1. 确保文件开头没有空格。
  2. 在http://www.corefiling.com/opensource/schemaValidate.html验证 XML 和 Schema
  3. 确保我的文件在没有BOM 的情况下进行编码。
  4. 在十六进制编辑器中仔细检查了 BOM 的缺失。XSD 的前三个字符是 3C 3F 78,XML 的前三个字符也是 3C 3F 78。
  5. 检查我的代码以确保我使用Load()而不是加载 XMLLoadXml()

但错误仍然存​​在。

我的 XML 加载函数显示:

public void LoadXml(ref XmlDocument target, string path)
{
    try
    {                
        target = new XmlDocument();                
        target.Load(path);
    }
    catch (XmlException ex)            
    {
        // Exception is thrown is there is an error in the Xml
        MessageBox.Show(string.Format("An XML Exception was thrown while loading the XML file from {0}.\nException text: {1}\nXML file line: {2}", path, ex.Message, ex.LineNumber));
        Application.Exit();
    }
    catch (Exception ex)             
    {
        // General exception
        MessageBox.Show(string.Format("A General Exception was thrown while loading the XML file from {0}.\nException text: {1}", path, ex.Message));
        Application.Exit();
    }            
}

还有我的验证功能:

private bool ValidateXml(string xmlPath, string schemaPath)
{
    try
    {
        XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
        xmlReaderSettings.ValidationEventHandler += XmlReaderValidationEventHandler;

        xmlReaderSettings.CloseInput = true;
        xmlReaderSettings.ValidationType = ValidationType.Schema;
        xmlReaderSettings.Schemas.Add(null, Settings.Default.SchemaPath);
        xmlReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings |
                                                XmlSchemaValidationFlags.ProcessIdentityConstraints |
                                                XmlSchemaValidationFlags.ProcessInlineSchema |
                                                XmlSchemaValidationFlags.ProcessSchemaLocation;
        StringReader sr = new StringReader(Settings.Default.PlcXmlPath);
        using (XmlReader validatingReader = XmlReader.Create(sr, xmlReaderSettings))
        {                    
            while (validatingReader.Read())
            {
                // Loop through the document
            }
        }

        return true;
    }
    catch (XmlSchemaValidationException ex)
    {
        MessageBox.Show(string.Format("Unable to validate XML file {0} against schema {1}\nException text: {2}\nXML Line: {3}\nData: {4}", xmlPath, schemaPath, ex.Message, ex.LineNumber, ex.Data));
        return false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("Unable to validate XML file {0} against schema {1}\nException text: {2}", xmlPath, schemaPath, ex.Message));
        return false;
    }
}

我已经将我的 XSD 和 XML 文件一直降低到几乎没有,但这个错误仍然存​​在。这是 XSD:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="MachineParameters">

    </xsd:element>
</xsd:schema>

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<MachineParameters>Test
</MachineParameters>

谁能发现我做错了什么,或者建议任何进一步的步骤来尝试?我真的很感谢你的帮助——我整天都在为此苦苦挣扎。

编辑问题的答案和其他可能有用的信息:

内部异常为空。堆栈跟踪是:在 System.Xml.XmlTextReaderImpl.Throw(Exception e)\r\n 在 System.Xml.XmlTextReaderImpl.Throw(String res, String arg)\r\n 在 System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()\ r\n 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent()\r\n 在 System.Xml.XmlTextReaderImpl.Read()\r\n 在 System.Xml.XsdValidatingReader.Read()\r\n 在 Configuration.PLCConfigurationTool。 PlcConfigurationData.ValidateXml(字符串 xmlPath,字符串 schemaPath)

编辑
我似乎已经通过在验证函数中用 StreamReader 替换 StringReader 来解决这个问题。

我相信,StringReader 只是在读取 XML 文件的路径,而不是实际读取文件。

4

2 回答 2

1

我似乎已通过在验证函数中将 StringReader 替换为 StreamReader 来解决此问题。

我相信,StringReader 只是在读取 XML 文件的路径,而不是实际读取文件。

于 2014-06-26T14:45:10.930 回答
0

您应该将 targetNamespace 属性添加到架构中,并将其添加到 xml 中的根 MachineParameters 节点。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://myschema">
.
.
.

<?xml version="1.0" encoding="utf-8"?>
<MachineParameters xmlns="myschema">Test
</MachineParameters>
于 2013-08-08T15:54:24.667 回答