Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,065 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,586 people online right now. Registration is fast and FREE... Join Now!




validate a xml string against XSD

 
Reply to this topicStart new topic

validate a xml string against XSD

antobx
23 Apr, 2008 - 12:49 AM
Post #1

New D.I.C Head
*

Joined: 23 Apr, 2008
Posts: 1

Hi ,
I create a xml string using a xmldocument (c# 2.0 )
This is my code (more or less)


csharp

XmlElement MySearchNode = xmlDoc.CreateElement("MySearch");
xmlDoc.AppendChild(MySearchNode);
...
XmlNode node1 = xmlDoc.SelectSingleNode("/MySearch/Objects");
XmlAttribute nameAttrib = xmlDoc.CreateAttribute("result");
nameAttrib.Value = "012";
node1.Attributes.Append(nameAttrib);
xmlDoc.outerxml

I want to validate the xml string against a XSD :
XmlReader rdr = null;
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
XmlTextReader readerXml = new XmlTextReader(p_xml, XmlNodeType.Element, context);
XmlTextReader readerSchema = new XmlTextReader(p_xsdPath);
XmlSchema schema = new XmlSchema();
schema = XmlSchema.Read(readerSchema, new

ValidationEventHandler(Schema_ValidationEventHandler));
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.ValidationType = ValidationType.Schema;
ReaderSettings.Schemas.Add(schema);
ReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;
ReaderSettings.ValidationEventHandler += new

ValidationEventHandler(settings_ValidationEventHandler);
rdr = XmlReader.Create(readerXml, ReaderSettings);



this is my XSD
CODE

(a part of it)
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="MySearch">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Objects" type="tObjects" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
      <xs:attribute name="result" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:complexType name="tObjects">
    <xs:sequence>
      <xs:element name="House" type="tHouse" minOccurs="0" maxOccurs="unbounded">
      </xs:element>
      <xs:element name="Garden" type="tGarden" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>


Note : this is my xml string
<MySearch>
<Objects result = '012' >
<House>
...
</House>
</Objects>
</MySearch>


when I validate I get this error :
The 'result' attribute is not declared.

I see that validation process need :

ReaderSettings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;

and

attributeFormDefault="unqualified" .

It doesn't work !! I always get the same error ..


Could you help me ? thanks in advance .

EDIT: Code blocks added

This post has been edited by PsychoCoder: 23 Apr, 2008 - 07:13 AM
User is offlineProfile CardPM
+Quote Post

zakary
RE: Validate A Xml String Against XSD
23 Apr, 2008 - 03:31 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
QUOTE(antobx @ 23 Apr, 2008 - 04:49 AM) *


<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MySearch">
<xs:complexType>
<xs:sequence>
<xs:element name="Objects" type="tObjects" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="result" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:complexType name="tObjects">
<xs:sequence>
<xs:element name="House" type="tHouse" minOccurs="0" maxOccurs="unbounded">
</xs:element>
<xs:element name="Garden" type="tGarden" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
....


try this in your xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
<xs:element name="MySearch">
<xs:complexType>
<xs:sequence>
<xs:element name="Objects" type="tObjects" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="result" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:complexType name="tObjects">
<xs:sequence>
<xs:element name="House" type="tHouse" minOccurs="0" maxOccurs="unbounded">
</xs:element>
<xs:element name="Garden" type="tGarden" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
....

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:41PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month