Lost
Lost

Reputation: 13645

How to load a schema without knowing the target namespace ahead of time?

I am referring to the code below for my XML validation from XSD and from a functional point of view it seems to be working fine.

var schemas = new XmlSchemaSet();
schemas.Add("http://microsoft.com/HealthCare/HL7/2X", xsdFilePath);

Boolean result = true;
xdocXml.Validate(schemas, (sender, e) =>
{
    result = false;
});

Now, as you can see I had to specify the schema name explicitly even though this information is self contained in XSD itself. Is there any way to extract that information from the XSD so that I do not have to specifically specify that?

Upvotes: 1

Views: 1819

Answers (1)

John Saunders
John Saunders

Reputation: 161801

Simply specify null for the namespace:

var schemas = new XmlSchemaSet();
schemas.Add(null, xsdFilePath);

From XmlSchemaSet.Add Method (String, String):

targetNamespace
Type: System.String
The schema targetNamespace property, or null to use the targetNamespace specified in the schema.

Upvotes: 5

Related Questions