C# 请帮助我将.NET 1.1 Xml验证代码转换为.NET 2.0

C# 请帮助我将.NET 1.1 Xml验证代码转换为.NET 2.0,c#,deprecated,asp.net-1.1,xml-validation,C#,Deprecated,Asp.net 1.1,Xml Validation,如果你能帮我摆脱下面这些警告,那就太好了。 我一直找不到一份好的文件。由于警告集中在private void ValidateConfiguration(XmlNode部分)部分,如果您以前遇到过这种情况,希望这不是很难回答 谢谢 private void ValidateConfiguration(XmlNode部分) { //如果没有配置节点,则抛出。 if(null==节) { 抛出新的ConfigurationException(“在…类中传递的配置节为

如果你能帮我摆脱下面这些警告,那就太好了。 我一直找不到一份好的文件。由于警告集中在
private void ValidateConfiguration(XmlNode部分)
部分,如果您以前遇到过这种情况,希望这不是很难回答

谢谢


private void ValidateConfiguration(XmlNode部分)
{                
//如果没有配置节点,则抛出。
if(null==节)
{
抛出新的ConfigurationException(“在…类中传递的配置节为null…必须定义一个配置文件。”,节);
}
//使用架构验证文档
XmlValidatingReader vreader=新的XmlValidatingReader(新的XmlTextReader(新的StringReader(section.OuterXml));
//在资源上打开流;XSD被设置为“嵌入式资源”,以便资源可以在其上打开流
使用(Stream xsdFile=XYZ.GetStream(“ABC.xsd”))
使用(StreamReader sr=新的StreamReader(xsdFile))
{
vreader.ValidationEventHandler+=新的ValidationEventHandler(ValidationCallBack);
添加(XmlSchema.Read(新的XmlTextReader(sr),null));
vreader.ValidationType=ValidationType.Schema;
//验证文档
while(vreader.Read()){}
如果(!\u isValidDocument)
{
_schemaErrors=_sb.ToString();
抛出新的ConfigurationException(“XML文档无效”);
}
}
}
//不会引起警告。
私有void ValidationCallBack(对象发送方,ValidationEventArgs参数)
{
//检查模式验证读取器有什么问题;

//在FX1.0上,它给出了一个警告“基本上,它告诉您使用
XmlReaderSettings
,而不是
XmlValidatingReader
,后者已被弃用

就个人而言,我不打算进行转换,我认为您实际这样做将有利于您的编码开发,因此这里有一些参考资料:

具体来看
XmlReader.Create()
方法的重载

然后查看与
XmlReaderSettings
类相关的不同属性:

试一试,看看会发生什么,如果你仍然有问题,问另一个问题:)

  • 替换
    
    抛出新的ConfigurationException(..)
    

    
    抛出新的ConfigurationErrorsException(..)
    

  • 替换
    XmlValidatingReader vreader=新的XmlValidatingReader(…)



  • 您是否尝试过进行警告中建议的更改?如果是,您是否有任何问题?我想他正在寻找如何进行这些更改。警告中提供的链接没有那么有用。您不需要从C#1.1转换到C#2.0(特别是因为没有C#1.1)。您需要从.NET 1.1转换到.NET 2.0。
    'System.Configuration.ConfigurationException.ConfigurationException(string)' is obsolete: 'This class is obsolete, to create a new exception create a System.Configuration!System.Configuration.ConfigurationErrorsException'   
    
    'System.Xml.XmlValidatingReader' is obsolete: 'Use XmlReader created by XmlReader.Create() method using appropriate XmlReaderSettings instead. http://go.microsoft.com/fwlink/?linkid=14202'    
    
    private void ValidateConfiguration( XmlNode section )
    {                
        // throw if there is no configuration node.
        if( null == section )
        {
            throw new ConfigurationException("The configuration section passed within the ... class was null ... there must be a configuration file defined.", section );
        }
        //Validate the document using a schema
        XmlValidatingReader vreader = new XmlValidatingReader( new XmlTextReader( new StringReader( section.OuterXml ) ) );
        //  open stream on Resources; the XSD is set as an "embedded resource" so Resource can open a stream on it
        using (Stream xsdFile = XYZ.GetStream("ABC.xsd"))
        using (StreamReader sr = new StreamReader(xsdFile))
        {
            vreader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            vreader.Schemas.Add(XmlSchema.Read(new XmlTextReader(sr), null));
            vreader.ValidationType = ValidationType.Schema;
            // Validate the document
            while (vreader.Read()) { }
    
            if (!_isValidDocument)
            {
                _schemaErrors = _sb.ToString();
                throw new ConfigurationException("XML Document not valid");
            }
        }
    }
    
    // Does not cause warnings.
    private void ValidationCallBack( object sender, ValidationEventArgs args )
    {
        //  check what KIND of problem the schema validation reader has;
        //  on FX 1.0, it gives a warning for "<xs:any...skip" sections.  Don't worry about those, only set validation false
        //  for real errors
        if( args.Severity == XmlSeverityType.Error )
        {
            _isValidDocument = false;
            _sb.Append( args.Message + Environment.NewLine );
        }
    }
    
    var vreader = XmlReader.Create(new StringReader(section.OuterXml), 
                                   new XmlReaderSettings
                                   {
                                      ValidationType = ValidationType.Schema
                                   });