SAXParser equivalent in C#

user437890 picture user437890 · Oct 11, 2010 · Viewed 16.5k times · Source

I have below java code , I need to convert these in C#, Kindly help me ..

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

Thanks

Answer

Darin Dimitrov picture Darin Dimitrov · Oct 11, 2010

C# native XML parser XmlReader doesn't support SAX and is forward-only. You may take a look at this article presenting some specific points about it. You could simulate a SAX parser using XmlReader. If it doesn't suit your needs you could also use XDocument which is a different API for working with XML files in .NET. So to conclude there's no push XML parser built into .NET framework so you might need to use a third party library or COM Interop to MSXML to achieve this if you really need an event driven parser.