Skip to main content
 首页 » 编程设计

c#之"Could not find schema information for the element"- 但我的架构文件确实包含该元素的信息

2024年02月23日15kuangbin

我正在尝试使用一个我没有源代码的应用程序。我正在尝试连接 XML 文件中的某些数据。我相信我的文件与应用程序所需的架构正确匹配,但我收到文件中每个元素的消息“无法找到该元素的架构信息”。我已经反编译应用程序代码并在 visual studio 中重新构建它,但我自己遇到了错误。

我将继续并转储代码...它很长,但我开始认为这是我的一个非常愚蠢的错误。

要导入的 XML:

<WorkOrders> 
    <WorkOrder> 
        <Action>SAVE</Action> 
        <UserDef1>NotRequired1</UserDef1> 
        <UserDef2>NotRequired2</UserDef2> 
        <UserDef3>NotRequired3</UserDef3> 
        <UserDef4>NotRequired4</UserDef4> 
        <UserDef5>NotRequired5</UserDef5> 
        <UserDef6>NotRequired6</UserDef6> 
        <UserDef7>-1</UserDef7> 
        <UserDef8>-2</UserDef8> 
        <AutoRelease>N</AutoRelease> <!--Probably can hardcode as N--> 
        <BuildInstructions>Not Required BuildInstructions</BuildInstructions> 
        <!--<BuildLocation>Not required BuildLocation</BuildLocation>--> 
        <Company></Company> <!--should match compan on the item master--> 
        <!--<DimensionUm></DimensionUm>--> 
        <DueDate>12-31-2011</DueDate> 
        <!--<Error></Error>--> 
        <FinishedItem>SERIAL_TEST</FinishedItem> 
        <QtyToBeBuilt>12345</QtyToBeBuilt> 
        <!--<QtyUm></QtyUm> 
        <RevisionNum></RevisionNum>--> 
        <Warehouse>PPLant</Warehouse> 
        <WeightUm></WeightUm> 
        <WOAllocMethod></WOAllocMethod> 
        <WorkOrderDetails> 
            <Action>SAVE</Action> 
            <UserDef1>NotRequired1</UserDef1> 
            <UserDef2>NotRequired2</UserDef2> 
            <UserDef3>NotRequired3</UserDef3> 
            <UserDef4>NotRequired4</UserDef4> 
            <UserDef5>NotRequired5</UserDef5> 
            <UserDef6>NotRequired6</UserDef6> 
            <UserDef7>-1</UserDef7> 
            <UserDef8>-2</UserDef8> 
            <AllocationRule>WorkOrderAllocation</AllocationRule> 
            <BuildLevel>1</BuildLevel> 
            <!--<BuildSequence>1</BuildSequence>--> 
            <Company></Company> 
            <ComponentItem>SERIAL_TEST</ComponentItem> 
            <FromLocation></FromLocation>--> 
            <!-- these fields allow users to increase/decrease the component quantity required 
            <IncQtyAmount></IncQtyAmount> 
            <IncQtyEligible></IncQtyEligible> 
            <IncQtyMinAmount></IncQtyMinAmount> 
            <IncQtyType></IncQtyType>--> 
            <!-- <LogisticsUnit></LogisticsUnit> 
            <Lot></Lot> 
            <ParentLogisticsUnit></ParentLogisticsUnit>--> 
            <QtyNeededPerItem>10</QtyNeededPerItem> 
            <!--<QuantityUm></QuantityUm>--> 
            <Warehouse>PPLant</Warehouse> 
        </WorkOrderDetails> 
        <WorkOrderId>ExampleWorkOrderID001</WorkOrderId> 
        <AllocationRule>WorkOrderAllocation</AllocationRule> 
 
    </WorkOrder> 
</WorkOrders> 

架构:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="WorkOrderDownload" targetNamespace="http://www.asdf.com/asdfNET/Interface" elementFormDefault="qualified" xmlns="http://www.asdf.com/asdfNET/Interface" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2008"> 
    <xs:include schemaLocation="InterfaceEntity.xsd" /> 
    <xs:element name="WorkOrders" nillable="false" type="WorkOrderList" /> 
    <xs:complexType name="WorkOrderList"> 
        <xs:sequence> 
            <xs:element minOccurs="1" maxOccurs="unbounded" name="WorkOrder" nillable="true" type="WorkOrder" /> 
        </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="WorkOrder"> 
        <xs:sequence> 
            <xs:element minOccurs="0" maxOccurs="1" name="AutoRelease" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="BuildInstructions" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="BuildLocation" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="Company" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="DimensionUm" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="DueDate" type="xs:dateTime" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="Error" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="FinishedItem" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="InterfaceEntity" type="InterfaceEntity" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="InternalWorkOrderNum" type="xs:int" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemClass" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemColor" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemCost"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemDescription" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemHeight"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemLength"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemSize" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemStyle" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemValue"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemWeight"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="ItemWidth"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="PlannedUnitBuildTime"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="QtyToBeBuilt"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="1" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="QtyUm" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="RevisionNum"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:int"> 
                        <xs:minInclusive value="1" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="VolumeUm" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="Warehouse" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="WeightUm" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="WOAllocMethod" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="WorkOrderDetails" type="WorkOrderDetailList" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="WorkOrderId" type="xs:string" /> 
        </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="WorkOrderDetailList"> 
        <xs:sequence> 
            <xs:element minOccurs="0" maxOccurs="unbounded" name="WorkOrderDetail" nillable="true" type="WorkOrderDetail" /> 
        </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="WorkOrderDetail"> 
        <xs:sequence> 
            <xs:element minOccurs="0" maxOccurs="1" name="AllocationRule" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="BuildLevel"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:int"> 
                        <xs:minInclusive value="1" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="BuildSequence"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:int"> 
                        <xs:minInclusive value="1" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="Company" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ComponentItem" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="FromLocation" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="IncQtyAmount" type="xs:double" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="IncQtyEligible" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="IncQtyMinAmount"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="IncQtyType" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="InterfaceEntity" type="InterfaceEntity" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="InternalWorkOrderNum"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:int"> 
                        <xs:minInclusive value="0" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="LogisticsUnit" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="Lot" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="ParentLogisticsUnit" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="QtyNeededPerItem"> 
                <xs:simpleType> 
                    <xs:restriction base="xs:decimal"> 
                        <xs:minInclusive value="1" /> 
                    </xs:restriction> 
                </xs:simpleType> 
            </xs:element> 
            <xs:element minOccurs="0" maxOccurs="1" name="QuantityUm" type="xs:string" /> 
            <xs:element minOccurs="0" maxOccurs="1" name="Warehouse" type="xs:string" /> 
        </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Schema; 
 
namespace WorkOrderInterfaceValidation 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            helper h = new helper(); 
            h.main(); 
        } 
    } 
 
    class helper 
    { 
        StringBuilder _valErrorMsgs; 
 
        public helper() 
        { 
            _valErrorMsgs = new StringBuilder(); 
        } 
 
        public void main() 
        { 
            try 
            { 
                string error = "No error"; 
                Console.WriteLine("hello world"); 
 
                ValidateSchemaWithURI("C:\\Program Files\\software\\asdf\\2010\\Interface\\Input\\ExampleWorkOrder001.xml", 
                    "http://www.asdf.com/asdfNET/Interface", 
                    "\\\\localhost\\asdf\\Schemas\\Interface\\WorkOrderDownload.xsd", 
                    out error 
                    ); 
 
                error += " err"; 
                Console.WriteLine(error); 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine(e.Message); 
            } 
 
            Console.ReadKey(); 
        } 
 
        public XmlReader ValidateSchemaWithURI(string inputUri, string targetNamespace, string schemaPath, out string error) 
        { 
            //StringBuilder _valErrorMsgs = new StringBuilder(); 
            XmlReader reader = null; 
            XmlReader reader2; 
            try 
            { 
                XmlReaderSettings settings = new XmlReaderSettings(); 
                settings.Schemas.Add(targetNamespace, schemaPath); 
                settings.IgnoreWhitespace = true; 
                settings.ValidationType = ValidationType.Schema; 
                settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; 
                settings.ValidationEventHandler += new ValidationEventHandler(this.SchemaValidationEventHandler); 
                reader = XmlReader.Create(inputUri, settings); 
 
                foreach (var v in settings.Schemas.Schemas()) 
                { 
                    foreach (var e in ((XmlSchema)v).Attributes) 
                    { 
                        e.ToString(); 
                    } 
                } 
 
                while (reader.Read()) 
                { 
 
                } 
                reader2 = reader; 
            } 
            catch (XmlException exception) 
            { 
                _valErrorMsgs.Append(exception.Message).Append(";"); 
                Console.WriteLine(exception.Message); 
                reader2 = null; 
            } 
            finally 
            { 
                error = _valErrorMsgs.ToString(); 
                if (reader != null) 
                { 
                    reader.Close(); 
                } 
            } 
            return reader2; 
        } 
 
        private void SchemaValidationEventHandler(object sender, ValidationEventArgs args) 
        { 
            this._valErrorMsgs.Append(args.Exception.Message).Append(";"); 
        } 
    } 
} 

错误信息:

XmlException: Could not find schema information for the element 'WorkOrders'.;Could not find schema information for the element 'WorkOrder'.;Could not find schema information for the element 'Action'.;Could not find schema information for the element 'UserDef1'.;Could not find schema information for the element 'UserDef2'.;Could not find schema information for the element 'UserDef3'.;Could not find schema information for the element 'UserDef4'.;Could not find schema information for the ....

请您参考如下方法:

我认为您缺少 namespace 。尝试:

<WorkOrders xmlns="http://www.manh.com/ILSNET/Interface">