2010-05-04 61 views
0

如果您可以帮助我摆脱下面的这些警告,那将会很棒。 我一直没有找到一个好的文档。由于警告集中在private void ValidateConfiguration(XmlNode section)部分,所以希望这不是很难回答,如果您以前遇到过这个问题。帮助我将.NET 1.1 Xml验证代码转换为.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); 
    } 
} 
+1

您是否尝试过制作中的警告建议的修改?如果是这样,你有什么问题吗? – 2010-05-04 22:37:46

+1

我认为他正在寻找如何进行这些改变。警告中提供的链接没有用处。 – 2010-05-04 22:38:43

+1

您不需要将C#1.1转换为C#2.0(特别是因为没有C#1.1这样的东西)。您需要将.NET 1.1转换为.NET 2.0。 – 2010-05-04 22:43:56

回答

1
  1. 更换 throw new ConfigurationException(....)

    throw new ConfigurationErrorsException(....)

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


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

基本上,它告诉你使用​​代替XmlValidatingReader,这是不推荐使用。

个人我不会做转换,我认为你实际上这样做将有利于您的编码开发,所以这里是一些资源:

看那XmlReader.Create()方法的重载,特别是this one

再看看在与​​类相关联的不同属性:http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings_members.aspx

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

HTH