SAXParser equivalent in C#
Asked Answered
H

2

7

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

Hyperdulia answered 11/10, 2010 at 6:35 Comment(0)
S
10

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.

Septuplet answered 11/10, 2010 at 6:38 Comment(1)
The op only wants to read configuration, but for the benefit of others who are interested in SAX parsing: You can generally not simulate a SAX parser with XmlReader because SAX's concepts are different: SAX gives me an "open element tag" even if the document is malformed and it never closes. XmlReader gives me the the whole subtree only when it's finished reading it, and must therefore be complete. There's another port of SAX here, but I'm not sure what it's worth.Canna
M
5

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

Memorialist answered 4/5, 2014 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.