3

XSD スキーマに対して XML ファイルを検証しようとしていますが、XmlException というメッセージが表示されます

ルート レベルのデータは無効です。行 1、位置 1。

以前の同様の投稿のクロールに基づいて、次のことを行いました。

  1. ファイルの先頭に空白がないことを確認しました。
  2. http://www.corefiling.com/opensource/schemaValidate.htmlで XML とスキーマを検証しました。
  3. ファイルがBOMなしでエンコードされていることを確認しました。
  4. 16 進エディタで BOM がないことを再確認しました。XSD の最初の 3 文字は 3C 3F 78 であり、XML の最初の 3 文字も 3C 3F 78 です。
  5. コードをチェックして、XML をロードしていることを確認しましたLoad()LoadXml()

しかし、エラーは解決しません。

私の 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>

誰かが私が間違ったことを見つけたり、試してみるためのさらなるステップを提案したりできますか? 私はあなたの助けに本当に感謝しています.私は一日中これに対して頭を悩ませてきました.

EDIT質問への回答、およびその他の潜在的に役立つ情報:

内部例外は null です。スタック トレースは次のとおりです。 r\n System.Xml.XmlTextReaderImpl.ParseDocumentContent() で\r\n System.Xml.XmlTextReaderImpl.Read() で\r\n System.Xml.XsdValidatingReader.Read() で\r\n Configuration.PLCConfigurationTool で。 PlcConfigurationData.ValidateXml(String xmlPath, String schemaPath)

編集
検証関数で StringReader を StreamReader に置き換えることで、この問題を修正したようです。

StringReader は、実際にファイルを読み取るのではなく、XML ファイルのパスを読み取るだけだったと思います。

4

2 に答える 2

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 に答える