Dim settings As XmlReaderSettings = New XmlReaderSettings() settings.Schemas.Add(Nothing, Server.MapPath( schemaFileName)) settings.ValidationType = ValidationType.Schema settings.ValidationFlags = settings.ValidationFlags和 XmlSchemaValidationFlags.ReportValidationWarnings AddHandler settings.ValidationEventHandler,New ValidationEventHandler(AddressOf ValidationEventHandler) Dim stringRdr As New StringReader(xmlCommandString) Dim xmlRdr As XmlReader xmlRdr = XmlReader.Create(stringRdr,settings) Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load(xmlRdr) 返回xmlDoc 结束函数 私有共享子ValidationEventHandler(ByVal发送者为对象, ByVal args As ValidationEventArgs) 抛出新的XmlSchemaException(schemaFileValidationErrorMessag e & args.Message) End Sub 我认为这个问题与这个属性的第一个实例相关 (在XMLCommand节点之上)但是我不知道在哪里/如何设置这个 的价值。当我试图在XMLDoc对象中加载xml时,它也不会让我删除而不会导致问题。有没有人有任何想法 造成这种情况的原因是什么? < xs:attribute name =" name"类型= QUOT; XS:串QUOT; /> Hi,I am trying to use a validation method against this xml and keepgetting ''The ''name'' attribute is not declared'' and I can''t figure outwhy. Here is my XML: <XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"connectionKey="SomeKey" commandTimeout="60" appId="122"appScreenType="879"><Proc name="SomeProcName"><Param name="@SomeParam" dataType="Char" dataLength="3"direction="Input">MHR</Param></Proc><Email><Success addresses="SomeEmail" subject="Some Subject"body="Some Body." /><Failure addresses="AnotherEmail;AndAnother" subject="SomeSubject." body="SomeBody" /></Email></XmlCommand> And here is my Schema: <?xml version="1.0" encoding="utf-8"?><xs:schema id="XmlCommand" elementFormDefault="qualified"version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd"><xs:attribute name="name" type="xs:string" /><xs:element name="XmlCommand"><xs:complexType><xs:sequence><xs:element name="Proc" minOccurs="1"><xs:complexType><xs:sequence><xs:element name="Param" nillable="true"><xs:complexType><xs:sequence><xs:any processContents="skip" /></xs:sequence><xs:attribute ref="name" use="required" /><xs:attribute name="dataType" use="optional"type="xs:string" /><xs:attribute name="dataLength" use="optional"type="xs:string" /><xs:attribute name="direction" use="optional"type="xs:string" /><xs:attribute name="precision" use="optional"type="xs:positiveInteger" /><xs:attribute name="scale" use="optional"type="xs:positiveInteger" /></xs:complexType></xs:element></xs:sequence><xs:attribute ref="name" use="required" /></xs:complexType></xs:element><xs:element name="Email" nillable="true"><xs:complexType><xs:sequence><xs:element name="Success"><xs:complexType><xs:sequence></xs:sequence><xs:attribute name="addresses" use="optional"type="xs:string" /><xs:attribute name="subject" use="optional"type="xs:string" /><xs:attribute name="body" use="optional"type="xs:string" /></xs:complexType></xs:element><xs:element name="Failure"><xs:complexType><xs:sequence></xs:sequence><xs:attribute name="addresses" use="optional"type="xs:string" /><xs:attribute name="subject" use="optional"type="xs:string" /><xs:attribute name="body" use="optional"type="xs:string" /></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:sequence><xs:attribute name="connectionKey" use="required"type="xs:string" /><xs:attribute name="commandTimeout" use ="optional"type="xs:positiveInteger" /><xs:attribute name="appId" use="optional"type="xs:positiveInteger" /><xs:attribute name="appScreenType" use="optional"type="xs:positiveInteger" /></xs:complexType></xs:element></xs:schema> and here is my code: Private Function LoadXml(ByVal xmlCommandString As String) AsXmlDocumentDim settings As XmlReaderSettings = NewXmlReaderSettings()settings.Schemas.Add(Nothing,Server.MapPath(schemaFileName))settings.ValidationType = ValidationType.Schemasettings.ValidationFlags = settings.ValidationFlags AndXmlSchemaValidationFlags.ReportValidationWarningsAddHandler settings.ValidationEventHandler, NewValidationEventHandler(AddressOf ValidationEventHandler) Dim stringRdr As New StringReader(xmlCommandString)Dim xmlRdr As XmlReaderxmlRdr = XmlReader.Create(stringRdr, settings) Dim xmlDoc As XmlDocument = New XmlDocument()xmlDoc.Load(xmlRdr) Return xmlDocEnd Function Private Shared Sub ValidationEventHandler(ByVal sender As Object,ByVal args As ValidationEventArgs)Throw New XmlSchemaException(schemaFileValidationErrorMessag e& args.Message)End SubI think the problem is related to the first instance of this attribute(above the XMLCommand node) but I don''t know where/how to set thisvalue. It also won''t let me remove it without causing issues when Itry to load the xml in the XMLDoc object. Does anyone have any ideaswhat is causing this? <xs:attribute name="name" type="xs:string" /> 推荐答案 " Doug" < dn ****** @ dtgnet.com写信息 新闻:11 ********************** @ d57g2000hsg .googlegr oups.com ... "Doug" <dn******@dtgnet.comwrote in messagenews:11**********************@d57g2000hsg.googlegr oups.com... 我正在尝试对这个xml使用验证方法并保持 得到'''''属性未被声明''我无法弄清楚 为什么。 这是我的XML: < XmlCommand xmlns =" http://intranet/hstServices/Schemas/XmlCommand.xsd" connectionKey =" SomeKey"的CommandTimeout = QUOT; 60" appId =" 122" appScreenType =" 879"> < Proc name =" SomeProcName"> < ; Param name =" @ SomeParam"的dataType = QUOT;字符" dataLength =" 3" direction =" Input"> MHR< / Param> < / proc> <电子邮件> < Success addresses =" SomeEmail" subject =" Some Subject" body =" Some Body。" /> < Failure addresses =" AnotherEmail; AndAnother" subject =" Some Subject。体= QUOT;某人" /> < / Email> < / XmlCommand> 这是我的架构: <?xml version =" 1.0" encoding =" utf-8"?> < xs:schema id =" XmlCommand" elementFormDefault =" qualified" version =" 1.0" xmlns:xs =" http://www.w3.org/2001/XMLSchema" targetNamespace =" http://intranet/hstServices/Schemas/XmlCommand.xsd" xmlns =" http://intranet/hstServices/Schemas/XmlCommand.xsd" xmlns:NS =" http://intranet/hstServices/Schemas/XmlCommand.xsd" > < xs:attribute name =" name"类型= QUOT; XS:串QUOT; /> < xs:element name =" XmlCommand"> < xs:complexType> < xs:序列> < xs:element name =" Proc" minOccurs =" 1"> < xs:complexType> < xs:sequence> < xs:元素名称= QUOT;帕拉姆" nillable =" true"> < xs:complexType> < xs:sequence> < xs:any processContents = QUOT;跳过" /> < / xs:sequence> < xs:attribute ref =" name"使用= QUOT;需" /> < xs:attribute name =" dataType" use =" optional" type =" xs:string" /> < xs:attribute name =" dataLength" use =" optional" type =" xs:string" /> < xs:attribute name =" direction" use =" optional" type =" xs:string" /> < xs:attribute name =" precision" use =" optional" type =" xs:positiveInteger" /> < xs:attribute name =" scale" use =" optional" type =" xs:positiveInteger" /> < / xs:complexType> < / xs:element> < / xs:sequence> < xs:attribute ref =" name"使用= QUOT;需" /> < / xs:complexType> < / xs:element> < xs:element name ="电子邮件" nillable =" true"> < xs:complexType> < xs:sequence> < xs:元素名称="成功> < xs:complexType> < xs:sequence> < / xs:sequence> < xs:attribute name =" addresses" use =" optional" type =" xs:string" /> < xs:attribute name =" subject" use =" optional" type =" xs:string" /> < xs:attribute name =" body" use =" optional" type =" xs:string" /> < / xs:complexType> < / xs:element> < xs:element name ="失败>> < xs:complexType> < xs:sequence> < / xs:sequence> < xs:attribute name =" addresses" use =" optional" type =" xs:string" /> < xs:attribute name =" subject" use =" optional" type =" xs:string" /> < xs:attribute name =" body" use =" optional" type =" xs:string" /> < / xs:complexType> < / xs:element> < / xs:sequence> < / xs:complexType> < / xs:element> < / xs:sequence> < xs:attribute name =" connectionKey" use =" required" type =" xs:string" /> < xs:attribute name =" commandTimeout" use =" optional" type =" xs:positiveInteger" /> < xs:attribute name =" appId" use =" optional" type =" xs:positiveInteger" /> < xs:attribute name =" appScreenType" use =" optional" type =" xs:positiveInteger" /> < / xs:complexType> < / xs:element> < / xs:schema> ,这是我的代码: Hi,I am trying to use a validation method against this xml and keepgetting ''The ''name'' attribute is not declared'' and I can''t figure outwhy.Here is my XML:<XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"connectionKey="SomeKey" commandTimeout="60" appId="122"appScreenType="879"> <Proc name="SomeProcName"> <Param name="@SomeParam" dataType="Char" dataLength="3"direction="Input">MHR</Param> </Proc> <Email> <Success addresses="SomeEmail" subject="Some Subject"body="Some Body." /> <Failure addresses="AnotherEmail;AndAnother" subject="SomeSubject." body="SomeBody" /> </Email></XmlCommand>And here is my Schema:<?xml version="1.0" encoding="utf-8"?><xs:schema id="XmlCommand" elementFormDefault="qualified"version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd"> <xs:attribute name="name" type="xs:string" /> <xs:element name="XmlCommand"> <xs:complexType> <xs:sequence> <xs:element name="Proc" minOccurs="1"> <xs:complexType> <xs:sequence> <xs:element name="Param" nillable="true"> <xs:complexType> <xs:sequence> <xs:any processContents="skip" /> </xs:sequence> <xs:attribute ref="name" use="required" /> <xs:attribute name="dataType" use="optional"type="xs:string" /> <xs:attribute name="dataLength" use="optional"type="xs:string" /> <xs:attribute name="direction" use="optional"type="xs:string" /> <xs:attribute name="precision" use="optional"type="xs:positiveInteger" /> <xs:attribute name="scale" use="optional"type="xs:positiveInteger" /> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute ref="name" use="required" /> </xs:complexType> </xs:element> <xs:element name="Email" nillable="true"> <xs:complexType> <xs:sequence> <xs:element name="Success"> <xs:complexType> <xs:sequence> </xs:sequence> <xs:attribute name="addresses" use="optional"type="xs:string" /> <xs:attribute name="subject" use="optional"type="xs:string" /> <xs:attribute name="body" use="optional"type="xs:string" /> </xs:complexType> </xs:element> <xs:element name="Failure"> <xs:complexType> <xs:sequence> </xs:sequence> <xs:attribute name="addresses" use="optional"type="xs:string" /> <xs:attribute name="subject" use="optional"type="xs:string" /> <xs:attribute name="body" use="optional"type="xs:string" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="connectionKey" use="required"type="xs:string" /> <xs:attribute name="commandTimeout" use ="optional"type="xs:positiveInteger" /> <xs:attribute name="appId" use="optional"type="xs:positiveInteger" /> <xs:attribute name="appScreenType" use="optional"type="xs:positiveInteger" /> </xs:complexType> </xs:element></xs:schema>and here is my code: 你的代码不是问题。您的架构未验证您的XML。以 a仔细查看我从.NET模式验证器获得的这些错误,这些错误由 Stylus Studio提供: ------ - 运行自定义验证引擎.NET XML Parser ... Doug-2007-04-20.xml:6,6:必需属性 ''http:// intranet / hstServices缺少/Schemas/XmlCommand.xsd:name''。 Doug-2007-04-20.xml:7,16:未声明''name''属性。 Doug-2007-04-20.xml:7,10:必需属性 ''http://intranet/hstServices/Schemas/XmlCommand.xsd:name''缺失。 Doug-2007-04-20.xml:7,83:命名空间中的元素''Param'' ''http:// intranet / hstServices / Schemas / XmlCommand.xsd''不能包含文本。 预期可能元素列表:''## any:*''。 Doug-2007-04-20 .xml:7,88:namespac中的元素''Param'' e $> ''http://intranet/hstServices/Schemas/XmlCommand.xsd''内容不完整。 预期可能元素列表:''## any: *''。 ---------------------------------- - 验证完成,有6个错误 您需要限定对全局名称属性的引用。尝试 在XML文件中为它定义名称空间前缀,然后用它来引用 名称: < XmlCommand xmlns =" ; http://intranet/hstServices/Schemas/XmlCommand.xsd" connectionKey =" SomeKey" xmlns:tns =" http:// intranet / hstServices /Schemas/XmlCommand.xsd" commandTimeout =" 60" APPID = QUOT; 122" appScreenType = QUOT; 879" > < Proc tns:name =" SomeProcName"> < Param tns:name =" @ SomeParam"的dataType = QUOT;字符" dataLength =" 3" direction =" Input"> MHR< / Param> < / proc> <电子邮件> < Success addresses =" SomeEmail" subject =Some subject body ="一些 Body。" /> < Failure addresses =" AnotherEmail; AndAnother" subject =" Some subject。" body =" SomeBody" /> < / Email> < / XmlCommand> - John Saunders [MVP] Your code isn''t the problem. Your XML is not validated by your schema. Takea close look at these errors I get from the .NET schema validator as run byStylus Studio:--------Running custom validation engine .NET XML Parser... Doug-2007-04-20.xml:6,11: The ''name'' attribute is not declared.Doug-2007-04-20.xml:6,6: The required attribute''http://intranet/hstServices/Schemas/XmlCommand.xsd:name'' is missing.Doug-2007-04-20.xml:7,16: The ''name'' attribute is not declared.Doug-2007-04-20.xml:7,10: The required attribute''http://intranet/hstServices/Schemas/XmlCommand.xsd:name'' is missing.Doug-2007-04-20.xml:7,83: The element ''Param'' in namespace''http://intranet/hstServices/Schemas/XmlCommand.xsd'' cannot contain text.List of possible elements expected: ''##any:*''.Doug-2007-04-20.xml:7,88: The element ''Param'' in namespace''http://intranet/hstServices/Schemas/XmlCommand.xsd'' has incomplete content.List of possible elements expected: ''##any:*''. -----------------------------------Validation complete with 6 error(s) You''ll need to qualify your references to the global name attribute. Trydefining a namespace prefix for it in your XML file, then use it to refer toname:<XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"connectionKey="SomeKey"xmlns:tns="http://intranet/hstServices/Schemas/XmlCommand.xsd"commandTimeout="60" appId="122" appScreenType="879" ><Proc tns:name="SomeProcName"><Param tns:name="@SomeParam" dataType="Char" dataLength="3"direction="Input">MHR</Param></Proc><Email><Success addresses="SomeEmail" subject="Some Subject" body="SomeBody."/><Failure addresses="AnotherEmail;AndAnother" subject="Some Subject."body="SomeBody"/></Email></XmlCommand>-- John Saunders [MVP] 你需要限定你对全局名称属性的引用。 当你说你的参考资格 属于这个属性时,我不确定你是什么意思。但是我尝试在下面的例子中添加了你在中的xmlns:tns条目,它仍然失败。 You''ll need to qualify your references to the global name attribute. I''m not sure what you mean by this when you say qualify your referenceto this attribute. But I tried adding the xmlns:tns entry you have inthe example below and it still failed. Doug写道: Doug wrote: 当你说你的参考资格 属于这个属性时,我不确定你的意思。但是我尝试在下面的例子中添加你在中的xmlns:tns条目,但它仍然失败了。 I''m not sure what you mean by this when you say qualify your referenceto this attribute. But I tried adding the xmlns:tns entry you have inthe example below and it still failed. 你想要定义什么?如果要定义名为name的 属性位于架构的目标命名空间中,那么在XML示例中需要将属性限定为例如 < Proc xmlns:pf =" http://intranet/hstServices/Schemas/XmlCommand.xsd" xmlns =" http:// intranet / hstServices / Schemas / XmlCommand.xsd" pf:name =" SomeProcName"> 如果该name属性应该在没有命名空间的话你不能将定义为目标命名空间模式中的全局属性 http://intranet/hstServices/Schemas/XmlCommand.xsd 。您需要 定义没有命名空间的模式并在其中定义属性,然后 您需要导入模式并引用该属性。或者你需要 来内联属性定义,就像你为其他 属性所做的那样,这样他们就不合格了。 - Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ What exactly do you want to define? If you want to define that theattribute named ''name'' is in the target namespace of your schema thenwithin the XML sample you need to qualify the attribute as e.g.<Proc xmlns:pf="http://intranet/hstServices/Schemas/XmlCommand.xsd"xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"pf:name="SomeProcName"> If that name attribute is supposed to be in no namespace then you can''tdefine it as a global attribute in a schema for the target namespace http://intranet/hstServices/Schemas/XmlCommand.xsd. You would need todefine a schema for no namespace and define the attribute in there, thenyou need to import the schema and reference the attribute. Or you needto inline the attribute definition as you have done for the otherattributes, that way they are unqualified. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ 这篇关于xml文件中的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-26 23:41