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

发布于 2024-08-31 04:21:16 字数 2318 浏览 1 评论 0原文

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

谢谢!

'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 );
    }
}

It would be fantastic if you could help me rid of these warnings below.
I have not been able to find a good document. Since the warnings are concentrated in just the private void ValidateConfiguration( XmlNode section ) section, hopefully this is not terribly hard to answer, if you have encountered this before.

Thanks!

'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 );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-09-07 04:21:16
  1. 替换
    <代码>
    抛出新的配置异常(....)

    <代码>
    抛出新的 ConfigurationErrorsException(....)
    替换

  2. 替换XmlValidatingReader vreader = new XmlValidatingReader(...)


var vreader = XmlReader.Create(new StringReader(section.OuterXml), 
                               new XmlReaderSettings
                               {
                                  ValidationType = ValidationType.Schema
                               });
  1. Replace

    throw new ConfigurationException(....)

    with


    throw new ConfigurationErrorsException(....)

  2. Replace XmlValidatingReader vreader = new XmlValidatingReader(...)
    with


var vreader = XmlReader.Create(new StringReader(section.OuterXml), 
                               new XmlReaderSettings
                               {
                                  ValidationType = ValidationType.Schema
                               });
半夏半凉 2024-09-07 04:21:16

基本上,它告诉您使用 XmlReaderSettings 而不是 XmlValidatingReader,后者已被弃用。

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

查看 XmlReader.Create() 的重载方法,特别是这个

然后查看与 XmlReaderSettings 类关联的不同属性:http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings_members.aspx

尝试一下,看看会发生什么,如果仍然遇到问题,请询问另一个问题:)

HTH

Basically, it's telling you to use the XmlReaderSettings instead of the XmlValidatingReader, which was deprecated.

Personally I'm not going to do the conversion, I think that you actually doing that will be good for your coding development, so here is some resources:

Look at the overloads of the XmlReader.Create() method, specifically this one.

Then have a look at the different properties associated with the XmlReaderSettings class: http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings_members.aspx

Give it a try, see what happens and if your still having problems, ask another question :)

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文