Example Code - Python+PythonNet, Ironpython, Boo

S

srijit

Hello,
Here is an example code of xml writer in Python+PythonNet, Ironpython
and Boo. The codes look very similar.

Regards,
Srijit

Python + PythonNet:

import CLR
import CLR.System
from CLR.System import Console as CLR_Console
import CLR.System.Xml as CLR_Xml

true = 1
false = 0
filename = "test1.xml"
writer = CLR_Xml.XmlTextWriter(filename, None);
#~ Use indenting for readability.
writer.Formatting = CLR_Xml.Formatting.Indented;

writer.WriteComment("XML in Boo Language");

#~ Write an element (this one is the root).
writer.WriteStartElement("EducationalCentres");

#~ Write the namespace declaration.
writer.WriteAttributeString("xmlns", "schname", None,
"urn:schoolnames");

writer.WriteStartElement("School");

#~ Lookup the prefix and then write the ISBN attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();

#~ Write the title.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();

#~ Write the price.
writer.WriteElementString("Students", "500");

#~ Write the style element.
writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
writer.WriteString("IGCSE");
writer.WriteEndElement();

#~ Write the end tag for the book element.
writer.WriteEndElement();

#~ Write the close tag for the root element.
writer.WriteEndElement();

#~ Write the XML to file and close the writer.
writer.Flush();
writer.Close();

#~ Read the file back in and parse to ensure well formed XML.
doc = CLR_Xml.XmlDocument();
#~ Preserve white space for readability.
doc.PreserveWhitespace = true;
#~ Load the file
doc.Load(filename);

#~ Write the XML content to the console.
CLR_Console.Write(doc.InnerXml);


Ironpython:

from System import *
from System.Xml import *

true = 1
false = 0
filename = "test1.xml"
writer = XmlTextWriter(filename, None);
#~ Use indenting for readability.
writer.Formatting = Formatting.Indented;

writer.WriteComment("XML in Boo Language");

#~ Write an element (this one is the root).
writer.WriteStartElement("EducationalCentres");

#~ Write the namespace declaration.
writer.WriteAttributeString("xmlns", "schname", None,
"urn:schoolnames");

writer.WriteStartElement("School");

#~ Lookup the prefix and then write the ISBN attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();

#~ Write the title.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();

#~ Write the price.
writer.WriteElementString("Students", "500");

#~ Write the style element.
writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
writer.WriteString("IGCSE");
writer.WriteEndElement();

#~ Write the end tag for the book element.
writer.WriteEndElement();

#~ Write the close tag for the root element.
writer.WriteEndElement();

#~ Write the XML to file and close the writer.
writer.Flush();
writer.Close();

#~ Read the file back in and parse to ensure well formed XML.
doc = XmlDocument();
#~ Preserve white space for readability.
doc.PreserveWhitespace = true;
#~ Load the file
doc.Load(filename);

#~ Write the XML content to the console.
Console.Write(doc.InnerXml);


Boo:

import System
import System.Xml from System.Xml

filename = "test1.xml"
writer = XmlTextWriter(filename, null);
#~ Use indenting for readability.
writer.Formatting = Formatting.Indented;

writer.WriteComment("XML in Boo Language");

#~ Write an element (this one is the root).
writer.WriteStartElement("EducationalCentres");

#~ Write the namespace declaration.
writer.WriteAttributeString("xmlns", "schname", null,
"urn:schoolnames");

writer.WriteStartElement("School");

#~ Lookup the prefix and then write the ISBN attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();

#~ Write the title.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();

#~ Write the price.
writer.WriteElementString("Students", "500");

#~ Write the style element.
writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
writer.WriteString("IGCSE");
writer.WriteEndElement();

#~ Write the end tag for the book element.
writer.WriteEndElement();

#~ Write the close tag for the root element.
writer.WriteEndElement();

#~ Write the XML to file and close the writer.
writer.Flush();
writer.Close();

#~ Read the file back in and parse to ensure well formed XML.
doc = XmlDocument();
#~ Preserve white space for readability.
doc.PreserveWhitespace = true;
#~ Load the file
doc.Load(filename);

#~ Write the XML content to the console.
Console.Write(doc.InnerXml);
 
S

Srijit Kumar Bhadra

Hello,
There are mistakes in the comments. I shall update it.

With regards,
Srijit
 
S

srijit

Hello,
There are mistakes in the comments. I shall correct it.

With regards,
Srijit
 
S

srijit

Hello,
Here is the code with modified comments.

Best Regards,
Srijit

Python + PythonNet:

import CLR
import CLR.System
from CLR.System import Console as CLR_Console
import CLR.System.Xml as CLR_Xml

true = 1
false = 0
filename = "test1.xml"
writer = CLR_Xml.XmlTextWriter(filename, None);
#~ Use indenting for readability.
writer.Formatting = CLR_Xml.Formatting.Indented;

writer.WriteComment("XML in Python with PythonNet");

#~ Write an element (this one is the root).
writer.WriteStartElement("EducationalCentres");

#~ Write the namespace declaration.
writer.WriteAttributeString("xmlns", "schname", None, "urn:schoolnames");

writer.WriteStartElement("School");

#~ Lookup the prefix and then write the School attribute..
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();

#~ Write the name of the city.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();

#~ Write number of students.
writer.WriteElementString("Students", "500");

#~ Write the style element.
writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
writer.WriteString("IGCSE");
writer.WriteEndElement();

#~ Write the end tag for the school element.
writer.WriteEndElement();

#~ Write the close tag for the root element.
writer.WriteEndElement();

#~ Write the XML to file and close the writer.
writer.Flush();
writer.Close();

#~ Read the file back in and parse to ensure well formed XML.
doc = CLR_Xml.XmlDocument();
#~ Preserve white space for readability.
doc.PreserveWhitespace = true;
#~ Load the file
doc.Load(filename);

#~ Write the XML content to the console.
CLR_Console.Write(doc.InnerXml);


Ironpython:

from System import *
from System.Xml import *

true = 1
false = 0
filename = "test1.xml"
writer = XmlTextWriter(filename, None);
#~ Use indenting for readability.
writer.Formatting = Formatting.Indented;

writer.WriteComment("XML in Ironpython");

#~ Write an element (this one is the root).
writer.WriteStartElement("EducationalCentres");

#~ Write the namespace declaration.
writer.WriteAttributeString("xmlns", "schname", None, "urn:schoolnames");

writer.WriteStartElement("School");

#~ Lookup the prefix and then write the School attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();

#~ Write the name of the city.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();

#~ Write number of students.
writer.WriteElementString("Students", "500");

#~ Write the style element.
writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
writer.WriteString("IGCSE");
writer.WriteEndElement();

#~ Write the end tag for the school element.
writer.WriteEndElement();

#~ Write the close tag for the root element.
writer.WriteEndElement();

#~ Write the XML to file and close the writer.
writer.Flush();
writer.Close();

#~ Read the file back in and parse to ensure well formed XML.
doc = XmlDocument();
#~ Preserve white space for readability.
doc.PreserveWhitespace = true;
#~ Load the file
doc.Load(filename);

#~ Write the XML content to the console.
Console.Write(doc.InnerXml);


Boo:

import System
import System.Xml from System.Xml

filename = "test1.xml"
writer = XmlTextWriter(filename, null);
// Use indenting for readability.
writer.Formatting = Formatting.Indented;

writer.WriteComment("XML in Boo Language");

// Write an element (this one is the root).
writer.WriteStartElement("EducationalCentres");

// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "schname", null, "urn:schoolnames");

writer.WriteStartElement("School");

// Lookup the prefix and then write the School attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();

// Write the name of the city.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();

// Write number of students.
writer.WriteElementString("Students", "500");

// Write the style element.
writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
writer.WriteString("IGCSE");
writer.WriteEndElement();

// Write the end tag for the school element.
writer.WriteEndElement();

// Write the close tag for the root element.
writer.WriteEndElement();

// Write the XML to file and close the writer.
writer.Flush();
writer.Close();

// Read the file back in and parse to ensure well formed XML.
doc = XmlDocument();
// Preserve white space for readability.
doc.PreserveWhitespace = true;
// Load the file
doc.Load(filename);

// Write the XML content to the console.
Console.Write(doc.InnerXml);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top