By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

default namespace / Xmlvalidatingreader

P: n/a
need some help on following:

xml file 1
<TEST xmlns="http://test" >
<OK>mlkddflmkj</OK>
</TEST>
xml file 2
<TEST>
<OK>mlkddflmkj</OK>
</TEST>
at runtime i add the necessary xsd schema's.
now, these are files i receive from customer, so i can't change them.
When i read in the xml file 1 , it works like, it is validates against
the xsd's, and everything works fine.
when i read the second one, it doesn't validate, if i make sure the xml

file isn't valid, the xmlvalidatingreader doesn't notice it. So i
think it has something to do with : xmlns="http://test" not being in
the second xml file.
So i tried to add it with a xmlparsercontext, but still it doesn't
work. How can i fix this?
the first xml file, goes trough this code and gives an exception when
file isn't valid, if the second xml file goes through this code, and
the file isn't valid it doesn't give an exception
public void Initialize(TextReader textReader)
{
XmlValidatingReader validatingReader = new
XmlValidatingReader(textReader);
validatingReader.Schemas.Add(XmlSchemaCache.GetIns tance().GetSchema("tralal*ala"));

validatingReader.Schemas.Add(schema);
validatingReader.ValidationType = ValidationType.Schema;
XmlReader readerToUse = validatingReader;
XPathDocument document = new XPathDocument(readerToUse); --> here it
throws exception if file isn't valid

}
this code i tried for the second xml file, so i add the default
namespace, but still i get no ecxeption when the xml file is invalid

public void Initialize(TextReader textReader)
{
XmlTextReader reader = new XmlTextReader(reader)
NameTable nt = reader.NameTable;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(reader.NameTable);

nsmgr.AddNamespace(String.Empty, "http://test");
XmlParserContext context = new XmlParserContext(reader.NameTable,
nsmgr, reader.XmlLang, reader.XmlSpace);
reader.MoveToContent();
XmlValidatingReader validatingReader = new
XmlValidatingReader(reader.ReadOuterXml(),XmlNodeT ype.Document,context);

validatingReader.Schemas.Add(XmlSchemaCache.GetIns tance().GetSchema("tralal*ala"));

validatingReader.Schemas.Add(schema);
validatingReader.ValidationType = ValidationType.Schema;
XmlReader readerToUse = validatingReader;
XPathDocument document = new XPathDocument(readerToUse); --> here it
DOES NOT throw exception if file isn't valid

}
now the funny thing is , in both functions when i put breakpoint on
this line:
XPathDocument document = new XPathDocument(readerToUse);
and check out the readerToUse object with QuickWatch, in the first
method above the default namespace isn't filled, but with the second
function it is filled (normal i think because i added it with the
xmlparsercontext).

So first i tought with adding the default namespace to the the
readerToUse object (in second function) it will work, till i saw that
the default namespace for the readerToUse object isn't filled in the
first function, so on what does the xmlvalidatingreader base itself for

validating the xmldocument???
i know this is a rather big and maybe difficult explanation of my
problem, so if there are questions please ask me, ANY help is weclom

Apr 11 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
You dont get an exception in the case of the second file, since the element
TEST does not belong to any defined schema. Meaning a schema with the
targetnamespace of this element could not be found. Target namespace in this
case is an empty namespace.

In this case, there is no exception thrown from the validation reader, but a
validation warning should be thrown. You should attach a
ValidationEventHandler to the XmlValidatingReader to view the validation
errors and warnings both. If you dont attach a handler, you will only see
validation errors (as Exceptions) and will not see warnings at all.

Zafar
"Plop69" <vo********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
need some help on following:

xml file 1
<TEST xmlns="http://test" >
<OK>mlkddflmkj</OK>
</TEST>
xml file 2
<TEST>
<OK>mlkddflmkj</OK>
</TEST>
at runtime i add the necessary xsd schema's.
now, these are files i receive from customer, so i can't change them.
When i read in the xml file 1 , it works like, it is validates against
the xsd's, and everything works fine.
when i read the second one, it doesn't validate, if i make sure the xml

file isn't valid, the xmlvalidatingreader doesn't notice it. So i
think it has something to do with : xmlns="http://test" not being in
the second xml file.
So i tried to add it with a xmlparsercontext, but still it doesn't
work. How can i fix this?
the first xml file, goes trough this code and gives an exception when
file isn't valid, if the second xml file goes through this code, and
the file isn't valid it doesn't give an exception
public void Initialize(TextReader textReader)
{
XmlValidatingReader validatingReader = new
XmlValidatingReader(textReader);
validatingReader.Schemas.Add(XmlSchemaCache.GetIns tance().GetSchema("tralal*
ala"));

validatingReader.Schemas.Add(schema);
validatingReader.ValidationType = ValidationType.Schema;
XmlReader readerToUse = validatingReader;
XPathDocument document = new XPathDocument(readerToUse); --> here it
throws exception if file isn't valid

}
this code i tried for the second xml file, so i add the default
namespace, but still i get no ecxeption when the xml file is invalid

public void Initialize(TextReader textReader)
{
XmlTextReader reader = new XmlTextReader(reader)
NameTable nt = reader.NameTable;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(reader.NameTable);

nsmgr.AddNamespace(String.Empty, "http://test");
XmlParserContext context = new XmlParserContext(reader.NameTable,
nsmgr, reader.XmlLang, reader.XmlSpace);
reader.MoveToContent();
XmlValidatingReader validatingReader = new
XmlValidatingReader(reader.ReadOuterXml(),XmlNodeT ype.Document,context);

validatingReader.Schemas.Add(XmlSchemaCache.GetIns tance().GetSchema("tralal*
ala"));

validatingReader.Schemas.Add(schema);
validatingReader.ValidationType = ValidationType.Schema;
XmlReader readerToUse = validatingReader;
XPathDocument document = new XPathDocument(readerToUse); --> here it
DOES NOT throw exception if file isn't valid

}
now the funny thing is , in both functions when i put breakpoint on
this line:
XPathDocument document = new XPathDocument(readerToUse);
and check out the readerToUse object with QuickWatch, in the first
method above the default namespace isn't filled, but with the second
function it is filled (normal i think because i added it with the
xmlparsercontext).

So first i tought with adding the default namespace to the the
readerToUse object (in second function) it will work, till i saw that
the default namespace for the readerToUse object isn't filled in the
first function, so on what does the xmlvalidatingreader base itself for

validating the xmldocument???
i know this is a rather big and maybe difficult explanation of my
problem, so if there are questions please ask me, ANY help is weclom
Apr 11 '06 #2

P: n/a
ok, i'll check that out. But how do i solve this programmatically?

i'm receiving such xml files, so one way or the other i have to add the
default namespace.

at the moment i read in the file, get the string , and manipulate the
string (so i replace <TEST> by <TEST xmlns="http://test" > ) and then i
push it to the xmlvalidatingreader.

but that can't be the best way to fix this, not?

Apr 12 '06 #3

P: n/a
ok, thats ok, i get the message :

Message "Could not find schema information for the element
'http://test:TEST'. An error occurred at ...'

now why is this not working? because the schema i add to the
xmlvalidatingreader "tralal**ala" , has this <xsd:schema
targetNamespace="'http://test" > , shouldn't the xmlvalidatinreader
think like this:

i have 'http://test:TEST' so i'll look in my schema's for a schema
with targetnamespace : 'http://test' ? not?

Apr 12 '06 #4

P: n/a
nobody?

Apr 13 '06 #5

P: n/a
If you are getting XML without a namespace then why dont you change your
schema to also not have a target namespace attribute. In that case it will
just validate without makeing any changes to the XML.

"Plop69" <vo********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
ok, i'll check that out. But how do i solve this programmatically?

i'm receiving such xml files, so one way or the other i have to add the
default namespace.

at the moment i read in the file, get the string , and manipulate the
string (so i replace <TEST> by <TEST xmlns="http://test" > ) and then i
push it to the xmlvalidatingreader.

but that can't be the best way to fix this, not?

Apr 14 '06 #6

P: n/a
your so absolutely right ;) thx very much, if i put the target
namespace in my schema on null, it works.

thx very much ;)

and now : WHY does that work, and why does adding the default namespace
like i tried before does not work? A bug in xmlvalidatingReader?
As far as my knowledge goes i think both options should work.

Apr 14 '06 #7

P: n/a
Doesn't it work if you also add the schemaLocation attribute to your
<TEST> element in your xml file?
So it looks like:
<TEST xmlns="http://test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://test theSchema.xsd">

Kind regards,
Thé Jethro ;)

Apr 14 '06 #8

P: n/a
I think both should work. XmlValidatingReader should behave correctly here.
That is: if your XML document has a default namespace declaration and you
also have a schema available in that namespace providing the element and
attribute declarations in the XML, it should validate. If not, please
provide a small repro for the problem.

Zafar
"Plop69" <vo********@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
your so absolutely right ;) thx very much, if i put the target
namespace in my schema on null, it works.

thx very much ;)

and now : WHY does that work, and why does adding the default namespace
like i tried before does not work? A bug in xmlvalidatingReader?
As far as my knowledge goes i think both options should work.

Apr 14 '06 #9

P: n/a
so Jethro, can u tell me how to add that attribute to my xml document?

don't forget i'm working with a XmlReader hu ;) so you can't write in
it, and i don't know how to add such SchemaLocation attribute with an
XmlParserContext object. The XSI namespace i can add via the
XmlParsercontext object, because there u can add a namespacemanager.

Apr 15 '06 #10

P: n/a
@Zafar , i'll provide a sample but give me some time :) it's weekend hi
:)

one last thing i want to say, it's working if i add my default
namespace to my xml document (test.xml) before i read it in memory.
It's not working if i'm reading in the file without a default
namespace, but later i add it with a xmlparsercontext:
reading in this file:
<TEST>
<OK>mlkddflmkj</OK>
</TEST>
public void Initialize(TextReader textReader)
{
XmlTextReader reader = new XmlTextReader(reader)
NameTable nt = reader.NameTable;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(reader.NameTable);

nsmgr.AddNamespace(String.Empty, "http://test");
XmlParserContext context = new XmlParserContext(reader.NameTable,
nsmgr, reader.XmlLang, reader.XmlSpace);
reader.MoveToContent();
XmlValidatingReader validatingReader = new
XmlValidatingReader(reader.ReadOuterXml(),XmlNodeT ype.Document,context);

validatingReader.Schemas.Add(XmlSchemaCache.GetIns tance().GetSchema("tralal**ala"));

validatingReader.Schemas.Add(schema);
validatingReader.ValidationType = ValidationType.Schema;
XmlReader readerToUse = validatingReader;
XPathDocument document = new XPathDocument(readerToUse); --> here it
DOES NOT throw exception if file isn't valid

}

Apr 15 '06 #11

P: n/a
@Jethro :
following on my previous answer,

xsi:schemaLocation="http://test theSchema.xsd">

if i add this schemalocation, the second part : the 'theSchema.xsd',
shouldn't that be a valid path to a file ? or am i wrong? if so that
won't work for me because my shema's are already read in a
XmlSchemaCollection cache, so when i read in a xml file i add the
necessary schema's to my xmlvalidatingreader::

validatingReader.Schemas.Add(XmlSchemaCache.GetIns tance().GetSchema("tralal**ala"));

validatingReader.Schemas.Add(schema);

grtz

Apr 15 '06 #12

P: n/a
here i made a small app: console application, just copy all in a class
and run the console app:

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace ConsoleAppXSDTestApp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

try
{
string sXSD = "<xs:schema
xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"
targetNamespace=\"http://www.testuri.com/test\"
elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\"
"

+ " <xs:element name=\"Test\">"
+ " <xs:annotation>"
+ " <xs:documentation>Comment describing your root
element</xs:documentation>"
+ " </xs:annotation>"
+ " <xs:complexType>"
+ " <xs:sequence>"
+ " <xs:element name=\"Person\">"
+ " <xs:complexType>"
+ " <xs:sequence>"
+ " <xs:element name=\"Name\">"
+ " <xs:simpleType>"
+ " <xs:restriction base=\"xs:string\">"
+ " <xs:minLength value=\"4\"/>"
+ " <xs:maxLength value=\"4\"/>"
+ " </xs:restriction>"
+ " </xs:simpleType>"
+ " </xs:element>"
+ " <xs:element name=\"Address\">"
+ " <xs:simpleType>"
+ " <xs:restriction base=\"xs:string\">"
+ " <xs:minLength value=\"7\"/>"
+ " <xs:maxLength value=\"7\"/>"
+ " </xs:restriction>"
+ " </xs:simpleType>"
+ " </xs:element>"
+ " </xs:sequence>"
+ " </xs:complexType>"
+ " </xs:element>"
+ " </xs:sequence>"
+ " </xs:complexType>"
+ " </xs:element>"
+ " </xs:schema>";

string sXmlWithDefault = "<Test
xmlns=\"http://www.testuri.com/test\">"
+ " <Person >"
+ " <Name>namee</Name>" //here the xsd file should give error,
only 4 characthers allowed by xsd
+ " <Address>address</Address>"
+ "</Person>"
+ "</Test>";

string sXmlWithoutDefault = "<Test>"
+ " <Person >"
+ " <Name>namee</Name>" //here the xsd file should give error,
only 4 characthers allowed by xsd
+ " <Address>address</Address>"
+ "</Person>"
+ "</Test>";
Console.WriteLine("Check XML - XSD");
// //TEST 1
// try
// {
// Console.WriteLine("Check with xml file with default
namespace...");
// XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithDefault));
// XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
// System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
// XmlValidatingReader reader = new XmlValidatingReader(xtrDocu);
// reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandl er);
// reader.Schemas.Add(schema);
// while(reader.Read()){};
// Console.WriteLine("success --> NOT EXPECTED");
// }
// catch(System.Exception)
// {
// Console.WriteLine("Crash As Expected");
// }
//END TEST 1

// //TEST 2
// try
// {
// Console.WriteLine("Check with xml file without default
namespace...");
// XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithoutDefault));
// XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
// System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
// XmlValidatingReader reader = new XmlValidatingReader(xtrDocu);
// reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandl er);
// reader.Schemas.Add(schema);
// while(reader.Read()){};
// Console.WriteLine("success --> NOT EXPECTED");
// }
// catch(System.Exception)
// {
// Console.WriteLine("Crash As Expected");
// }
// //END TEST 2
// //TEST 3
// try
// {
// Console.WriteLine("Check with xml file without default
namespace...");
// XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithoutDefault));
// XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
// System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
//
// XmlNamespaceManager nspmgr = new
System.Xml.XmlNamespaceManager(xtrDocu.NameTable);
// nspmgr.AddNamespace(string.Empty,"http://www.testuri.com/test");
//default namespace added
// XmlParserContext context = new
XmlParserContext(xtrDocu.NameTable,nspmgr,xtrDocu. XmlLang,xtrDocu.XmlSpace);
//
// XmlValidatingReader reader = new XmlValidatingReader(new
StringReader(sXmlWithoutDefault).ReadToEnd(),XmlNo deType.Document,context);
// reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandl er);
// reader.Schemas.Add(schema);
// while(reader.Read()){};
// Console.WriteLine("success --> NOT EXPECTED");
// }
// catch(System.Exception)
// {
// Console.WriteLine("Crash As Expected");
// }
// //END TEST 3
//TEST 4
try
{
Console.WriteLine("Check with xml file without default
namespace...");
XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithoutDefault));
XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
schema.TargetNamespace = null;
XmlValidatingReader reader = new XmlValidatingReader(xtrDocu);
reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandl er);
reader.Schemas.Add(schema);
while(reader.Read()){};
Console.WriteLine("success --> NOT EXPECTED");
}
catch(System.Exception)
{
Console.WriteLine("Crash As Expected");
}
//END TEST 4
}
catch(System.Exception ex)
{
Console.WriteLine("Ex: " + ex.Message);
}
finally
{
Console.ReadLine();
}
}

static void reader_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs eventArgs)
{
Console.WriteLine("Error: " + eventArgs.Message);
Console.ReadLine();
}
}
}
//end of test app ;)

to run the test, put all the test in comment except the one you would
like to run ;) (not the best app, but had to be done quick)

now, you can see there are 3 test cases.
TEST 1 : gives error as expected.
TEST 2: does not give error : offcourse default namespace isn't added
in the file -> validater does not find xsd (i think that's the reason)
TEST 3: does not give error , i don't understand, because i add default
namespace -> it's the same namespace as the target namespace in XSD
TEST 4: gives error, no default namespace in file, but i remove
targetnamespace in XSD (i do not understand why this works, but TEST 3
not.)

that's it, can you explain :)
in TEST 3 , i'm working with a xml file without default namespace, so i
add in the hope he will find my xsd that way, but he doesn't, so he
also do not report an error.

Apr 15 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.