默认名称空间/ Xmlvalidatingreader [英] default namespace / Xmlvalidatingreader

查看:60
本文介绍了默认名称空间/ Xmlvalidatingreader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要以下方面的帮助:


xml文件1

< TEST xmlns =" http:// test" >

< OK> mlkddflmkj< / OK>

< / TEST>

xml文件2

< TEST>

< OK> mlkddflmkj< / OK>

< / TEST>

在运行时添加必要的xsd schema''s。

现在,这些是我从客户收到的文件,因此我无法更改它们。

当我在xml文件中读取1时,它就像是,这是对xsd'的确认,并且一切正常。

当我读第二个时,它没有验证,如果我确保xml

文件无效,xmlvalidatingreader没有注意到它。所以我认为它与bml有关:xmlns =" http:// test"不在第二个xml文件中。

所以我试图用xmlparsercontext添加它,但它仍然没有
工作。我该如何解决这个问题?

第一个xml文件,通过这段代码并在

文件无效时给出异常,如果第二个xml文件通过这段代码,并且

文件无效,它没有例外

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); - >在这里它

如果文件无效则抛出异常


}

此代码我尝试用于第二个xml文件,所以我添加了默认的

命名空间,但是当xml文件无效时我仍然没有ecxeption


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); - >在这里它

如果文件无效则不抛出异常


}

现在有趣的是,两者都是函数当我把断点放在

这行时:

XPathDocument document = new XPathDocument(readerToUse);

并使用QuickWatch查看readerToUse对象,在第一个

方法上面,默认命名空间没有填充,但是第二个

函数它被填充(正常我认为因为我添加了

xmlparsercontext)。


首先,我想将默认命名空间添加到

readerToUse对象(在第二个函数中)它会工作,直到我看到

readerToUse对象的默认命名空间没有填入

第一个函数,那么xmlvalidatingreader基于什么


验证xmldocument ???

i知道这是一个相当大的,可能很困难的解释我的

问题em,所以如果有问题请问我,任何帮助都是weclom

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

推荐答案

你不会得到例外的情况第二个文件,因为元素

TEST不属于任何已定义的模式。意味着无法找到具有此元素的

targetnamespace的模式。这个

情况下的目标命名空间是一个空名称空间。


在这种情况下,验证读取器没有抛出异常,但是
应该抛出
验证警告。你应该将一个

ValidationEventHandler附加到XmlValidatingReader来查看验证

错误和警告。如果您没有附加处理程序,您将只看到

验证错误(作为例外)并且根本不会看到警告。


Zafar

" Plop69" < VO ******** @ gmail.com>在消息中写道

新闻:11 ********************** @ i39g2000cwa.googlegr oups.com ...

需要以下帮助:


xml文件1

< TEST xmlns =" http:// test" >

< OK> mlkddflmkj< / OK>

< / TEST>

xml文件2

< TEST>

< OK> mlkddflmkj< / OK>

< / TEST>

在运行时添加必要的xsd schema''s。

现在,这些是我从客户收到的文件,因此我无法更改它们。

当我在xml文件中读取1时,它就像是,这是对xsd'的确认,并且一切正常。

当我读第二个时,它没有验证,如果我确保xml

文件无效,xmlvalidatingreader没有注意到它。所以我认为它与bml有关:xmlns =" http:// test"不在第二个xml文件中。

所以我试图用xmlparsercontext添加它,但它仍然没有
工作。我该如何解决这个问题?

第一个xml文件,通过这段代码并在

文件无效时给出异常,如果第二个xml文件通过这段代码,并且

文件无效,它没有例外

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); - >在这里它

如果文件无效则抛出异常


}

此代码我尝试用于第二个xml文件,所以我添加了默认的

命名空间,但是当xml文件无效时我仍然没有ecxeption


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&quo t;));


validatingReader.Schemas.Add(schema);

validatingReader.ValidationType = ValidationType.Schema;

XmlReader readerToUse = validatingReader;

XPathDocument document = new XPathDocument(readerToUse); - >在这里它

如果文件无效则不抛出异常


}

现在有趣的是,两者都是函数当我把断点放在

这行时:

XPathDocument document = new XPathDocument(readerToUse);

并使用QuickWatch查看readerToUse对象,在第一个

方法上面,默认命名空间没有填充,但是第二个

函数它被填充(正常我认为因为我添加了

xmlparsercontext)。


首先,我想将默认命名空间添加到

readerToUse对象(在第二个函数中)它会工作,直到我看到

readerToUse对象的默认命名空间没有填入

第一个函数,那么xmlvalidatingreader基于什么


验证xmldocument ???

i知道这是一个相当大的,可能很困难的解释我的

问题em,所以如果有问题请问我,任何帮助都是weclom
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


好的,我会检查出来的。但是我如何以编程方式解决这个问题呢?


我收到这样的xml文件,所以无论如何我必须添加

默认命名空间。


此刻我在文件中读取,获取字符串,并操纵

字符串(所以我将< TEST>替换为< TEST xmlns =" http:// test">)然后我将
推送到xmlvalidatingreader。


但这不可能是解决这个问题的最佳方法,不是吗?

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?


好的,没关系,我收到的消息:


留言无法找到元素的架构信息

''http:// test:TEST''。发生错误......''


现在为什么这不起作用?因为我添加到

xmlvalidatingreader" tralal ** ala" ,有这个< xsd:schema

targetNamespace ="''http:// test" > ,不应该是xmlvalidatinreader

这样想:


i有''http:// test:TEST''所以我会看看我的模式'是模式的

与targetnamespace:''http:// test''?不是吗?

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?


这篇关于默认名称空间/ Xmlvalidatingreader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆