Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAXParser equivalent in C#

Tags:

c#

saxparser

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

like image 786
user437890 Avatar asked Oct 11 '10 06:10

user437890


2 Answers

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.

like image 135
Darin Dimitrov Avatar answered Nov 02 '22 13:11

Darin Dimitrov


I used SAX for .NET in two projects successfully in the past. http://saxdotnet.sourceforge.net/

like image 30
takuki Avatar answered Nov 02 '22 13:11

takuki