Hello All,
I'm currently trying to integrate python script in c#. Goal of this script is to parse the xml data. I have given my code below. So whenever I run the project I get below error:Error:No module named xml.parsers.expat
The error will be same if I use minidom/ElementTree
Program.cs
----------
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using IronPython.Hosting; using Microsoft.Scripting.Hosting; using IronPython.Modules; using IronPython.Runtime; using IronPython.Compiler; using IronPython.Zlib; using IronPython.SQLite; namespace PyCsharpApp3 class Program static void Main(string[] args) var ipy = Python.CreateRuntime(); dynamic test = ipy.UseFile( " Test.py" ); test.start_element(); Console.ReadLine(); test.py
--------
import xml.parsers.expat # 3 handler functions def start_element(name, attrs): print( ' Start element:' , name, attrs) def end_element(name): print( ' End element:' , name) def char_data(data): print( ' Character data:' , repr(data)) p = xml.parsers.expat.ParserCreate() p.StartElementHandler = start_element p.EndElementHandler = end_element p.CharacterDataHandler = char_data p.Parse( " " " <?xml version=" 1 . 0 " ?> <parent id=" top " ><child1 name=" paul " >Text goes here</child1> <child2 name=" fred " >More text</child2> </parent>" " " , 1 )
What I have tried:
Referred this link How solve exception in IronPython with Parse and Import? [ ^ ]
But still facing the same issue. Please help The IronPython implementation does not come with xml.parsers.expat . However, there is a workaround. There is a project FePy [ ^ ] that provides enhancements for IronPython. And xml.parsers.expat is one of them. Download this expat implementation file [ ^ ] and copy it to Lib\xml\parsers\expat.py in your IronPython installation folder.
Thanks for the reply. In addition to expat.py I have also copied below 2 files.
"c:/Python27/lib/xml/dom/expatbuilder.py" to "c:/Program Files (x86)/IronPython 2.7/lib/xml/dom"
"c:/Python27/lib/xml/sax/expatreader.py" to "c:/Program Files (x86)/IronPython 2.7/lib/xml/sax"

One more thing... Can we use lxml module with IronPython? From my initial research I found that it is not possible to use lxml with IronPython. What's the best ways to parse xml files using IronPython? expat or cElementTree or any other ..in terms of time and memory usage?
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •