具有嵌入式HTML和验证的XML模式

时间:2015-01-14 13:44:44

标签: c# xml xsd

我有这样的XML:

<Question id="question1" type="text" required="true">
  <Ask><b>Enter</b>some required text (required)</Ask>
</Question>

<Question id="question2" type="text" required="false">
  <Ask>Enter some text (optional)</Ask>
</Question>

我想允许在<Ask>标记内嵌入html。

所以我在我的XSD中试过这个

 <xs:element maxOccurs="unbounded" name="Question">
     <xs:complexType>
        <xs:sequence>
           <xs:element name="Ask">
               <xs:complexType mixed="true">
                   <xs:sequence>
                       <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
           </xs:sequence>               
      </xs:complexType>
 </xs:element>

但是,当读者解析它时,会抛出此错误:

System.Xml.Schema.XmlSchemaValidationException: The element 'Ask' has invalid child element 'b'. List of possible elements expected: any element in namespace 'http://www.w3.org/2001/XMLSchema'. 
at System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(ValidationEventHandler eventHandler, Object sender, XmlSchemaValidationException e, XmlSeverityType severity) 
at System.Xml.Schema.XmlSchemaValidator.ElementValidationError(XmlQualifiedName name, ValidationState context, ValidationEventHandler eventHandler, Object sender, String sourceUri, Int32 lineNo, Int32 linePos, XmlSchemaSet schemaSet) 
at System.Xml.Schema.XmlSchemaValidator.ValidateElementContext(XmlQualifiedName elementName, Boolean& invalidElementInContext) 
at System.Xml.Schema.XmlSchemaValidator.ValidateElement(String localName, String namespaceUri, XmlSchemaInfo schemaInfo, String xsiType, String xsiNil, String xsiSchemaLocation, String xsiNoNamespaceSchemaLocation) 
at System.Xml.XsdValidatingReader.ProcessElementEvent() 
at System.Xml.XsdValidatingReader.ProcessReaderEvent() 
at System.Xml.XsdValidatingReader.Read() 
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) 
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) 
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) 
at System.Xml.XmlDocument.Load(XmlReader reader) 
at CEUManager.Core.Survey.QuestionsFile.LoadXML(String xmlContents) in c:\Users\eric\Documents\Visual Studio 2013\Projects\Conexsys\CEUManager\CEUManager.Core\Survey\QuestionsFile.cs:line 591 
at CEUWebsiteFinal.Part1.OnInit(EventArgs e) in c:\Users\eric\Documents\Visual Studio 2013\Projects\Conexsys\CEUManager\CEUWebsiteFinal\Part1.aspx.cs:line 97

我找到了这个链接,但它似乎没有用?

XML Schema that allows embedding html

1 个答案:

答案 0 :(得分:2)

请改为尝试:

<xs:complexType mixed="true">
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:any processContents="lax" />
  </xs:choice>
</xs:complexType>

或者看你必须使用CDATA

<Question id="question1" type="text" required="true">
  <Ask><![CDATA[<b>Enter</b>some required text (required)]]></Ask>
</Question>

哪个可能是更好的选择