Click here to Skip to main content
15,914,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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
----------
C#
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
Posted
Updated 25-Dec-16 23:13pm
Comments
Richard MacCutchan 26-Dec-16 2:56am    
Where is the xml.parsers.expat module?

1 solution

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.
 
Share this answer
 
Comments
pgshsf 26-Dec-16 22:15pm    
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?
Thomas Daniels 27-Dec-16 0:49am    
I haven't tried it, but I believe using lxml should work, unless it appears to use modules that aren't in IronPython.

I have not too much experience with parsing XML in Python, so I can't give a comparison of modules.
pgshsf 27-Dec-16 2:58am    
Okay. Thanks for the reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900