本文介绍了默认名称空间/ 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

推荐答案




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

09-01 21:03