6

我有下面的java代码,我需要在C#中转换它们,请帮助我..

public class Configuration {

  private ConfigContentHandler confHandler;

  public Configuration() {
  }

  public boolean parseConfigFile() throws Exception {
    boolean bReturn = true;

    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();

    System.out.println("*** Start parsing");

    try {
       confHandler = new ConfigContentHandler(100);
       // Configuration file must be located in main jar file folder

       // Set the full Prosper file name
       String sConfigFile = "configuration.xml";

       // Get abstract (system independent) filename
       File fFile = new File(sConfigFile);

       if (!fFile.exists()) {
          System.out.println("Could not find configuration file " + sConfigFile + ", trying input parameters.");
          bReturn = false;
       }  else if (!fFile.canRead()) {
          System.out.println("Could not read configuration file " + sConfigFile + ", trying input parameters.");
          bReturn = false;
       } else {
          parser.parse(fFile, confHandler);
       }

    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Input error.");
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("*** End parsing");
    return bReturn;
  }

谢谢

4

2 回答 2

9

C# 本机 XML 解析器XmlReader不支持 SAX,并且是只进的。您可以看一下这篇文章,其中介绍了一些关于它的具体观点。您可以使用 XmlReader 模拟 SAX 解析器。如果它不满足您的需求,您还可以使用XDocument,它是一种用于在 .NET 中处理 XML 文件的不同 API。因此,总而言之,.NET 框架中没有内置推送 XML 解析器,因此如果您确实需要事件驱动的解析器,您可能需要使用第三方库或 COM 互操作到 MSXML 来实现这一点。

于 2010-10-11T06:38:19.897 回答
5

过去,我在两个项目中成功地使用了 SAX for .NET。 http://saxdotnet.sourceforge.net/

于 2014-05-04T15:50:56.850 回答