Connecting Tech Pros Worldwide Forums | Help | Site Map

validate non W3C schemas in C# NET2.0

Newbie
 
Join Date: Dec 2008
Posts: 4
#1: Dec 9 '08
Can anyone tell me how to validate xml files which are not W3C complaint in C#.NET.
my xml file is of this format
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" ?>
  2. <Test xmlns="x-schema:MySchema.xml" xmlns:Test="x-schema:TestSchema.xml" xmlns:Testid="x-schema:TestIdSchema.xml">
  3.     <Info>
  4.         <Information name="Myname" address="2"/>        
  5.     </Info>
  6. </Test>

Newbie
 
Join Date: Dec 2008
Posts: 4
#2: Dec 9 '08

re: validate non W3C schemas in C# NET2.0


Can anyone tell me how to validate xml schemas in C# .NET 2.0 which are not W3C complaint
my xml fiel is like this:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" ?>
  2. <Test xmlns="x-schema:MySchema.xml" xmlns:Test="x-schema:TestSchema.xml" xmlns:Testid="x-schema:TestIdSchema.xml">
  3.     <Info>
  4.         <Information name="Myname" address="2"/>        
  5.     </Info>
  6. </Test>
Regards,
Sushma
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,131
#3: Dec 9 '08

re: validate non W3C schemas in C# NET2.0


XML files are validated using an XML schema.

(Definition for XML schema: A schema is a way to describe and validate data in an XML environment)

A schema is a model for describing the structure of information...

Use an instance of the XmlSchemaValidator class to validate XML in C#.

-Frinny
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,131
#4: Dec 9 '08

re: validate non W3C schemas in C# NET2.0


Sushma, please do not double post your question.
It makes it hard for you to get an answer to your question.
When you have the time please read over the rules of the forum outlined in the Posting Guidelines, especially the section labeled Do Not Double Post Your Question.

I have merged your threads together into one thread.

-Moderator Frinny
Newbie
 
Join Date: Dec 2008
Posts: 4
#5: Dec 10 '08

re: validate non W3C schemas in C# NET2.0


Frinavale,
XmlSchemaValidator is for XSD file validation and my XML is of type XDR. I have used XMLValidatingReader to validate this eventhough it is obselate in .NET 2.0 as
XMLSchemaSet doesnt support XDR format .
Following is my input Test.xml
Expand|Select|Wrap|Line Numbers
  1.     <?xml version="1.0" ?>
  2.    <Test xmlns="x-schema:MySchema.xml" xmlns:Test="x-schema:TestSchema.xml" xmlns:Testid="x-schema:TestIdSchema.xml">
  3.       <Info>
  4.            <Information name="Myname" address="2"/>        
  5.        </Info>
  6.    </Test>
  7.  
validation part i have done like this:
Expand|Select|Wrap|Line Numbers
  1.             XmlSchemaCollection xsc = new XmlSchemaCollection();
  2.             xsc.Add("x-schema:MySchema.xml", @"C:\MySchema.xml");
  3.             xsc.Add("x-schema:TestSchema.xml", @"C:\TestSchema.xml");
  4.              XmlTextReader tr = new XmlTextReader(@"C:\Test.xml");
  5.             XmlValidatingReader vr = new XmlValidatingReader(tr);
  6.             vr.ValidationType = ValidationType.XDR;
  7.             vr.Schemas.Add(xsc);
  8.             vr.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
  9.             while (vr.Read()) ;
  10.  
I want to read attribute values of "name" and "address" from input xml. Please answer how to proceed further.
can i use XMLDocument to do this using "SelectSingleNode" and "Attributes.GetNamedItem"? for this i need to assign validated schemas to XMLDocument object. but XMLDocument object uses XMLSchemaSet while XmlValidatingReader uses XmlSchemaCollection.

Please send me sample code to parse this XML document.
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#6: Dec 10 '08

re: validate non W3C schemas in C# NET2.0


Rather than continuing with the obsoleted XDR format is there any reason why you cannot convert your XDR into an XSD? You could then use the updated methods. I believe there are tools that can help you to do this.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Dec 10 '08

re: validate non W3C schemas in C# NET2.0


I don't see any difference in the snippit provided with any other form of xml?
Reply