SlideShare a Scribd company logo
1 of 43
XML Writers
XML documents are text-based files
The XML Writer Programming Interface:
An XML writer represents a component that provides a fast, forward-only way of outputting XML
data to streams or files.
void CreateXmlFile(String[] theArray, string filename)
{
StringBuilder sb = new StringBuilder("");
// Loop through the array and build the file
sb.Append("<array>");
foreach(string s in theArray)
{
sb.Append("<element value="");
sb.Append(s);
sb.Append(""/>");
}
sb.Append("</array>");
// Create the file
StreamWriter sw = new StreamWriter(filename);
sw.Write(sb.ToString());
sw.Close();
}
Let's rewrite our sample file using .NET XML writers, as shown in the following code. A
.NET XML writer features ad hoc write methods for each possible XML node type and
makes the creation of XML output more logical and much less dependent on the
intricacies, and even the quirkiness, of the markup languages.
void CreateXmlFileUsingWriters(String[] theArray,
string filename)
{
// Open the XML writer (default encoding charset)
XmlTextWriter xmlw = new XmlTextWriter(filename, null);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("array");
foreach(string s in theArray)
{
xmlw.WriteStartElement("element");
xmlw.WriteAttributeString("value", s);
xmlw.WriteEndElement();
}
xmlw.WriteEndDocument();
// Close the writer
xmlw.Close();
}
<?xml version="1.0"?>
<array>
<element value="Rome" />
<element value="New York" />
<element value="Sydney" />
<element value="Stockholm" />
<element value="Paris" />
</array>
An XML writer is a specialized class that knows only how to write XML data to a variety
of storage media. It features ad hoc methods to write any special item that
characterizes XML documents—from character entities to processing instructions, from
comments to attributes, and from element nodes to plain text. In addition, and more
important, an XML writer guarantees well-formed XML 1.0–compliant output. And you
don't have to worry about a single angle bracket or the last element node that you left
open.
The XmlWriter Base Class
XML writers are based on the XmlWriter abstract class that defines the .NET
Framework interface for writing XML. The XmlWriter class is not directly creatable from
user applications, but it can be used as a reference type for objects that are instances
of classes derived from XmlWriter. Actually, the .NET Framework provides just one
class that gives a concrete implementation of the XmlWriter interface—the
XmlTextWriter class.
Writing Well-Formed XML Text
TheXmlTextWriter class takes a number of precautions to ensure that the final XML
code is perfectly compliant with the XML 1.0 standard of well-formedness. In particular,
the class verifies that any special character found in the passed text is automatically
escaped and that no elements are written in the wrong order (such as attributes outside
nodes, or CDATA sections within attributes). Finally, the Close method performs a full
check of well-formedness immediately prior to return. If the verification is successful,
the method ends gracefully; otherwise, an exception is thrown.
Other controls that the XmlTextWriter class performs on the generated XML output
ensure that each document starts with the standard XML prolog, shown in the following
code, and that any DOCTYPE node always precedes the document root node:
<?xml version="1.0" ?>
The following code demonstrates how to write two identical attributes for a specified
node:
xmlw.WriteStartElement("element");
xmlw.WriteAttributeString("value", s);
xmlw.WriteAttributeString("value", s);
xmlw.WriteEndElement();
In the check made just before dumping data out, the writer neither verifies the names
and semantics of the attributes nor validates the schema of the resultant document,
thus authorizing this code to generate bad XML.
Building an XML Document
Initialize the document
Write data
Close the document
Writing the XML Prolog
Once you have a living and functional instance of the XmlTextWriter class, the first XML
element you add to it is the official XML 1.0 signature. You obtain this signature in a
very natural and transparent way simply by calling the WriteStartDocument method.
This method starts a new document and marks the XML declaration with the version
attribute set to "1.0", as shown in the following code:
// produces: <?xml version="1.0"?>
writer.WriteStartDocument();
Decoding Base64 and BinHex Data
Reading encoded data is a bit trickier, but not because the ReadBase64 and
ReadBinHex methods feature a more complex interface. The difficulty lies in the fact
that you have to allocate a buffer to hold the data and make some decision about its
size. If the buffer is too large, you can easily waste memory; if the buffer is too small,
you must set up a potentially lengthy loop to read all the data. In addition, if you can't
process data as you read it, you need another buffer or stream in which you can
accumulate incoming data.
Encoding-derived classes also providea method—GetString—totransforman arrayof
bytes into a string, as shownhere:
XmlTextReader reader = new XmlTextReader(filename);
while(reader.Read())
{
if (reader.LocalName == "element")
{
byte[] bytes = new byte[1000];
int n = reader.ReadBase64(bytes, 0, 1000);
string buf = Encoding.Unicode.GetString(bytes);
// Output the decoded data
Console.WriteLine(buf.Substring(0,n));
}
}
reader.Close();
Embedding Images in XML Documents
The structure of the sample XML document is extremely simple. It will consist of a
single <jpeg> node holding the BinHex data plus an attribute containing the original
name, as shown here:
writer.WriteStartDocument();
writer.WriteComment("Contains a BinHex JPEG image");
writer.WriteStartElement("jpeg");
writer.WriteAttributeString("FileName", filename);
// Get the size of the file
FileInfo fi = new FileInfo(jpegFileName);
int size = (int) fi.Length;
// Read the JPEG file
byte[] img = new byte[size];
FileStream fs = new FileStream(jpegFileName, FileMode.Open);
BinaryReader f = new BinaryReader(fs);
img = f.ReadBytes(size);
f.Close();
// Write the JPEG data
writer.WriteBinHex(img, 0, size);
// Close the document
writer.WriteEndElement();
writer.WriteEndDocument();
public void WriteContent(DataTable dt)
{
// Write data
Writer.WriteStartElement("rs", "data", null);
foreach(DataRow row in dt.Rows)
{
Writer.WriteStartElement("z", "row", null);
foreach(DataColumn dc in dt.Columns)
Writer.WriteAttributeString(dc.ColumnName,
row[dc.ColumnName].ToString());
Writer.WriteEndElement();
}
Writer.WriteEndElement();
}
ADO Recordset objects do not support embedding more result sets in a single XML file.
For this reason, you must either develop a new XML format or use separate files, one
for each result set
Testing the XmlRecordsetWriter Class
For .NET Framework applications, using the XmlRecordsetWriter class is no big deal.
You simply instantiate the class and call its methods, as shown here:
void ButtonLoad_Click(object sender, System.EventArgs e)
{
// Create and display the XML document
CreateDocument("adors.xml");
UpdateUI("adors.xml");
}
void CreateDocument(string filename)
{
DataSet ds = LoadDataFromDatabase();
XmlRecordsetWriter writer = new
XmlRecordsetWriter(filename);
writer.WriteRecordset(ds);
}
A Read/Write XML Streaming Parser
XML readers and writers work in separate compartments and in an extremely
specialized way. Readers just read, and writers just write. There is no way to force
things to go differently, and in fact, the underlying streams are read-only or write-only
as required. Suppose that your application manages lengthy XML documents that
contain rather volatile data. Readers provide a powerful and effective way to read that
contents.
Designing a Writer on Top of a Reader
In the .NET Framework, the XML DOM classes make intensive use of streaming
readers and writers to build the in-memory tree and to flush it out to disk. Thus, readers
and writers are definitely the only XML primitives available in the .NET Framework.
Consequently, to build up a sort of lightweight XML DOM parser, we can only rely, once
more, on readers and writers
The inspiration for designing such a read/write streaming parser is database server
cursors. With database server cursors, you visit records one after the next and, if
needed, can apply changes on the fly. Database changes are immediately effective,
and actually the canvas on which your code operates is simply the database table. The
same model can be arranged to work with XML documents.
You will use a normal XML (validating) reader to visit the nodes in sequence. While
reading, however, you are given the opportunity to change attribute values and node
contents. Unlike the XML DOM, changes will have immediate effect. How can you
obtain these results? The idea is to use an XML writer on top of the reader
Built-In Support for Read/Write
Operations
When I first began thinking about this lightweight XML DOM component, one of key
points I identified was an efficient way to copy (in bulk) blocks of nodes from the readonly
stream to the write stream. Luckily enough, two somewhat underappreciated
XmlTextWriter methods just happen to cover this tricky but boring aspect of two-way
streaming: WriteAttributes and WriteNode.
The WriteAttributes method reads all the attributes available on the currently selected
node in the specified reader. It then copies them as a single string to the current output
stream. Likewise, the WriteNode method does the same for any other type of node.
Note that WriteNode does nothing if the node type is XmlNodeType.Attribute
The following code shows how to use these methods to create a copy of the original
XML file, modified to skip some nodes. The XML tree is visited in the usual node-first
approach using an XML reader. Each node is then processed and written out to the
associated XML writer according to the index. This code scans a document and writes
out every other node
XmlTextReader reader = new XmlTextReader(inputFile);
XmlTextWriter writer = new XmlTextWriter(outputFile);
// Configure reader and writer
writer.Formatting = Formatting.Indented;
reader.MoveToContent();
// Write the root
writer.WriteStartElement(reader.LocalName);
// Read and output every other node
int i=0;
while(reader.Read())
{
if (i % 2)
writer.WriteNode(reader, false);
i++;
}
// Close the root
writer.WriteEndElement();
// Close reader and writer
writer.Close();
reader.Close();
The CSV Reader/Writer in Action
Let's take a sample CSV file, read it, and apply some changes to the contents so that
they will automatically be persisted when the reader is closed. Here is the source CSV
file:
LastName,FirstName,Title,Country
Davolio,Nancy,Sales Representative,USA
Fuller,Andrew,Sales Manager,USA
Leverling,Janet,Sales Representative,UK
Suyama,Michael,Sales Representative,UK
// Instantiate the reader on a CSV file
XmlCsvReadWriter reader;
reader = new XmlCsvReadWriter("employees.csv",
hasHeader.Checked);
reader.EnableOutput = true;
reader.Read();
// Define the schema of the table to bind to the grid
DataTable dt = new DataTable();
for(int i=0; i<reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
DataColumn col = new DataColumn(reader.Name,
typeof(string));
dt.Columns.Add(col);
}
reader.MoveToElement();
// Loop through the CSV rows and populate the DataTable
do
{
DataRow row = dt.NewRow();
for(int i=0; i<reader.AttributeCount; i++)
{
if (reader[i] == "Sales Representative")
reader[i] = "Sales Force";
row[i] = reader[i].ToString();
}
dt.Rows.Add(row);
}
while (reader.Read());
// Flushes the changes to disk
reader.Close();
// Bind the table to the grid
dataGrid1.DataSource = dt;
Readers and writers are at the foundation of every I/O operation in the .NET
Framework. You find them at work when you operate on disk and on network files,
when you serialize and deserialize, while you perform data access, even when you
read and write configuration settings.
XML writers are ad hoc tools for creating XML documents using a higherlevel metaphor
and putting more abstraction between your code and the markup. By using XML
writers, you go far beyond markup to reach a nodeoriented dimension in which, instead
of just accumulating bytes in a block of contiguous memory, you assemble nodes and
entities to create the desired schema and infoset
.NET XML writers only ensure the well-formedness of each individual XML element
being generated. Writers can in no way guarantee the well-formedness of the entire
document and can do even less to validate a document against a DTD or a schema.
Although badly formed XML documents can only result from actual gross programming
errors, the need for an extra step of validation is often felt in production environments,
especially when the creation of the document depends on a number of variable factors
and run-time conditions. For this reason, we've also examined the key points involved
in the design and implementation of a validating XML writer.

More Related Content

What's hot (20)

JAXB
JAXBJAXB
JAXB
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
6 xml parsing
6   xml parsing6   xml parsing
6 xml parsing
 
JAXP
JAXPJAXP
JAXP
 
Sax parser
Sax parserSax parser
Sax parser
 
Lect35 javascript
Lect35 javascriptLect35 javascript
Lect35 javascript
 
06 xml processing-in-.net
06 xml processing-in-.net06 xml processing-in-.net
06 xml processing-in-.net
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
Generics
GenericsGenerics
Generics
 
DOSUG XML Beans overview by Om Sivanesian
DOSUG XML Beans overview by Om SivanesianDOSUG XML Beans overview by Om Sivanesian
DOSUG XML Beans overview by Om Sivanesian
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
Utilized JAXB to generate POJOs automatically
Utilized JAXB to generate POJOs automaticallyUtilized JAXB to generate POJOs automatically
Utilized JAXB to generate POJOs automatically
 
XML parsing using jaxb
XML parsing using jaxbXML parsing using jaxb
XML parsing using jaxb
 
Ajax
AjaxAjax
Ajax
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
5 xml parsing
5   xml parsing5   xml parsing
5 xml parsing
 
Jaxb
JaxbJaxb
Jaxb
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
 
JAXB: Create, Validate XML Message and Edit XML Schema
JAXB: Create, Validate XML Message and Edit XML SchemaJAXB: Create, Validate XML Message and Edit XML Schema
JAXB: Create, Validate XML Message and Edit XML Schema
 

Viewers also liked

Viewers also liked (11)

Xidi ancient villages (西遞古鎮)
Xidi ancient villages (西遞古鎮)Xidi ancient villages (西遞古鎮)
Xidi ancient villages (西遞古鎮)
 
caves marti i serdà
caves marti i serdàcaves marti i serdà
caves marti i serdà
 
öxidos metálicos.
öxidos metálicos.öxidos metálicos.
öxidos metálicos.
 
Nilai TKM 2012014 XI AK-1
Nilai TKM 2012014 XI AK-1Nilai TKM 2012014 XI AK-1
Nilai TKM 2012014 XI AK-1
 
X dvideojuegos
X dvideojuegosX dvideojuegos
X dvideojuegos
 
Xi seminario genetica 2016
Xi seminario genetica 2016Xi seminario genetica 2016
Xi seminario genetica 2016
 
Xiamen FD company brochure
Xiamen FD company brochureXiamen FD company brochure
Xiamen FD company brochure
 
Xiomy trabajo
Xiomy trabajoXiomy trabajo
Xiomy trabajo
 
Xein CV
Xein CVXein CV
Xein CV
 
畅聊XMPP/Jabber
畅聊XMPP/Jabber畅聊XMPP/Jabber
畅聊XMPP/Jabber
 
Xochimilco - gasolinerías transas
Xochimilco - gasolinerías transasXochimilco - gasolinerías transas
Xochimilco - gasolinerías transas
 

Similar to Xml writers (20)

Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
Chapter 18
Chapter 18Chapter 18
Chapter 18
 
XML
XMLXML
XML
 
Tool Development 04 - XML
Tool Development 04 - XMLTool Development 04 - XML
Tool Development 04 - XML
 
treeview
treeviewtreeview
treeview
 
treeview
treeviewtreeview
treeview
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
Simple xml in .net
Simple xml in .netSimple xml in .net
Simple xml in .net
 
XML Tutor maXbox starter27
XML Tutor maXbox starter27XML Tutor maXbox starter27
XML Tutor maXbox starter27
 
Sql Summit Clr, Service Broker And Xml
Sql Summit   Clr, Service Broker And XmlSql Summit   Clr, Service Broker And Xml
Sql Summit Clr, Service Broker And Xml
 
Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Applied xml programming for microsoft 3
Applied xml programming for microsoft 3Applied xml programming for microsoft 3
Applied xml programming for microsoft 3
 
Extensible markup language attacks
Extensible markup language attacksExtensible markup language attacks
Extensible markup language attacks
 
Xml serialization
Xml serializationXml serialization
Xml serialization
 
xml2tex at TUG 2014
xml2tex at TUG 2014xml2tex at TUG 2014
xml2tex at TUG 2014
 
15. text files
15. text files15. text files
15. text files
 

More from Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Selection sort
Selection sortSelection sort
Selection sortRaghu nath
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithmsRaghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp roleRaghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 

More from Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Perl
PerlPerl
Perl
 

Recently uploaded

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

Xml writers

  • 2. XML documents are text-based files The XML Writer Programming Interface: An XML writer represents a component that provides a fast, forward-only way of outputting XML data to streams or files.
  • 3. void CreateXmlFile(String[] theArray, string filename) { StringBuilder sb = new StringBuilder(""); // Loop through the array and build the file sb.Append("<array>"); foreach(string s in theArray) { sb.Append("<element value=""); sb.Append(s); sb.Append(""/>"); }
  • 4. sb.Append("</array>"); // Create the file StreamWriter sw = new StreamWriter(filename); sw.Write(sb.ToString()); sw.Close(); }
  • 5.
  • 6. Let's rewrite our sample file using .NET XML writers, as shown in the following code. A .NET XML writer features ad hoc write methods for each possible XML node type and makes the creation of XML output more logical and much less dependent on the intricacies, and even the quirkiness, of the markup languages.
  • 7. void CreateXmlFileUsingWriters(String[] theArray, string filename) { // Open the XML writer (default encoding charset) XmlTextWriter xmlw = new XmlTextWriter(filename, null); xmlw.Formatting = Formatting.Indented;
  • 8. xmlw.WriteStartDocument(); xmlw.WriteStartElement("array"); foreach(string s in theArray) { xmlw.WriteStartElement("element"); xmlw.WriteAttributeString("value", s); xmlw.WriteEndElement(); } xmlw.WriteEndDocument(); // Close the writer xmlw.Close(); }
  • 9. <?xml version="1.0"?> <array> <element value="Rome" /> <element value="New York" /> <element value="Sydney" /> <element value="Stockholm" /> <element value="Paris" /> </array>
  • 10. An XML writer is a specialized class that knows only how to write XML data to a variety of storage media. It features ad hoc methods to write any special item that characterizes XML documents—from character entities to processing instructions, from comments to attributes, and from element nodes to plain text. In addition, and more important, an XML writer guarantees well-formed XML 1.0–compliant output. And you don't have to worry about a single angle bracket or the last element node that you left open.
  • 11. The XmlWriter Base Class XML writers are based on the XmlWriter abstract class that defines the .NET Framework interface for writing XML. The XmlWriter class is not directly creatable from user applications, but it can be used as a reference type for objects that are instances of classes derived from XmlWriter. Actually, the .NET Framework provides just one class that gives a concrete implementation of the XmlWriter interface—the XmlTextWriter class.
  • 12. Writing Well-Formed XML Text TheXmlTextWriter class takes a number of precautions to ensure that the final XML code is perfectly compliant with the XML 1.0 standard of well-formedness. In particular, the class verifies that any special character found in the passed text is automatically escaped and that no elements are written in the wrong order (such as attributes outside nodes, or CDATA sections within attributes). Finally, the Close method performs a full check of well-formedness immediately prior to return. If the verification is successful, the method ends gracefully; otherwise, an exception is thrown.
  • 13. Other controls that the XmlTextWriter class performs on the generated XML output ensure that each document starts with the standard XML prolog, shown in the following code, and that any DOCTYPE node always precedes the document root node: <?xml version="1.0" ?>
  • 14. The following code demonstrates how to write two identical attributes for a specified node: xmlw.WriteStartElement("element"); xmlw.WriteAttributeString("value", s); xmlw.WriteAttributeString("value", s); xmlw.WriteEndElement(); In the check made just before dumping data out, the writer neither verifies the names and semantics of the attributes nor validates the schema of the resultant document, thus authorizing this code to generate bad XML.
  • 15. Building an XML Document Initialize the document Write data Close the document
  • 16. Writing the XML Prolog Once you have a living and functional instance of the XmlTextWriter class, the first XML element you add to it is the official XML 1.0 signature. You obtain this signature in a very natural and transparent way simply by calling the WriteStartDocument method. This method starts a new document and marks the XML declaration with the version attribute set to "1.0", as shown in the following code: // produces: <?xml version="1.0"?> writer.WriteStartDocument();
  • 17. Decoding Base64 and BinHex Data Reading encoded data is a bit trickier, but not because the ReadBase64 and ReadBinHex methods feature a more complex interface. The difficulty lies in the fact that you have to allocate a buffer to hold the data and make some decision about its size. If the buffer is too large, you can easily waste memory; if the buffer is too small, you must set up a potentially lengthy loop to read all the data. In addition, if you can't process data as you read it, you need another buffer or stream in which you can accumulate incoming data.
  • 18. Encoding-derived classes also providea method—GetString—totransforman arrayof bytes into a string, as shownhere: XmlTextReader reader = new XmlTextReader(filename); while(reader.Read()) { if (reader.LocalName == "element") { byte[] bytes = new byte[1000]; int n = reader.ReadBase64(bytes, 0, 1000);
  • 19. string buf = Encoding.Unicode.GetString(bytes); // Output the decoded data Console.WriteLine(buf.Substring(0,n)); } } reader.Close();
  • 20. Embedding Images in XML Documents The structure of the sample XML document is extremely simple. It will consist of a single <jpeg> node holding the BinHex data plus an attribute containing the original name, as shown here: writer.WriteStartDocument(); writer.WriteComment("Contains a BinHex JPEG image"); writer.WriteStartElement("jpeg"); writer.WriteAttributeString("FileName", filename);
  • 21. // Get the size of the file FileInfo fi = new FileInfo(jpegFileName); int size = (int) fi.Length; // Read the JPEG file byte[] img = new byte[size]; FileStream fs = new FileStream(jpegFileName, FileMode.Open); BinaryReader f = new BinaryReader(fs); img = f.ReadBytes(size); f.Close();
  • 22. // Write the JPEG data writer.WriteBinHex(img, 0, size); // Close the document writer.WriteEndElement(); writer.WriteEndDocument();
  • 23. public void WriteContent(DataTable dt) { // Write data Writer.WriteStartElement("rs", "data", null); foreach(DataRow row in dt.Rows) { Writer.WriteStartElement("z", "row", null); foreach(DataColumn dc in dt.Columns) Writer.WriteAttributeString(dc.ColumnName, row[dc.ColumnName].ToString()); Writer.WriteEndElement(); } Writer.WriteEndElement(); }
  • 24. ADO Recordset objects do not support embedding more result sets in a single XML file. For this reason, you must either develop a new XML format or use separate files, one for each result set
  • 25. Testing the XmlRecordsetWriter Class For .NET Framework applications, using the XmlRecordsetWriter class is no big deal. You simply instantiate the class and call its methods, as shown here: void ButtonLoad_Click(object sender, System.EventArgs e) { // Create and display the XML document CreateDocument("adors.xml"); UpdateUI("adors.xml"); } void CreateDocument(string filename) { DataSet ds = LoadDataFromDatabase(); XmlRecordsetWriter writer = new XmlRecordsetWriter(filename); writer.WriteRecordset(ds); }
  • 26. A Read/Write XML Streaming Parser XML readers and writers work in separate compartments and in an extremely specialized way. Readers just read, and writers just write. There is no way to force things to go differently, and in fact, the underlying streams are read-only or write-only as required. Suppose that your application manages lengthy XML documents that contain rather volatile data. Readers provide a powerful and effective way to read that contents.
  • 27. Designing a Writer on Top of a Reader In the .NET Framework, the XML DOM classes make intensive use of streaming readers and writers to build the in-memory tree and to flush it out to disk. Thus, readers and writers are definitely the only XML primitives available in the .NET Framework. Consequently, to build up a sort of lightweight XML DOM parser, we can only rely, once more, on readers and writers
  • 28. The inspiration for designing such a read/write streaming parser is database server cursors. With database server cursors, you visit records one after the next and, if needed, can apply changes on the fly. Database changes are immediately effective, and actually the canvas on which your code operates is simply the database table. The same model can be arranged to work with XML documents.
  • 29. You will use a normal XML (validating) reader to visit the nodes in sequence. While reading, however, you are given the opportunity to change attribute values and node contents. Unlike the XML DOM, changes will have immediate effect. How can you obtain these results? The idea is to use an XML writer on top of the reader
  • 30. Built-In Support for Read/Write Operations When I first began thinking about this lightweight XML DOM component, one of key points I identified was an efficient way to copy (in bulk) blocks of nodes from the readonly stream to the write stream. Luckily enough, two somewhat underappreciated XmlTextWriter methods just happen to cover this tricky but boring aspect of two-way streaming: WriteAttributes and WriteNode.
  • 31. The WriteAttributes method reads all the attributes available on the currently selected node in the specified reader. It then copies them as a single string to the current output stream. Likewise, the WriteNode method does the same for any other type of node. Note that WriteNode does nothing if the node type is XmlNodeType.Attribute
  • 32. The following code shows how to use these methods to create a copy of the original XML file, modified to skip some nodes. The XML tree is visited in the usual node-first approach using an XML reader. Each node is then processed and written out to the associated XML writer according to the index. This code scans a document and writes out every other node
  • 33. XmlTextReader reader = new XmlTextReader(inputFile); XmlTextWriter writer = new XmlTextWriter(outputFile); // Configure reader and writer writer.Formatting = Formatting.Indented; reader.MoveToContent(); // Write the root writer.WriteStartElement(reader.LocalName);
  • 34. // Read and output every other node int i=0; while(reader.Read()) { if (i % 2) writer.WriteNode(reader, false); i++; } // Close the root writer.WriteEndElement(); // Close reader and writer writer.Close(); reader.Close();
  • 35. The CSV Reader/Writer in Action Let's take a sample CSV file, read it, and apply some changes to the contents so that they will automatically be persisted when the reader is closed. Here is the source CSV file: LastName,FirstName,Title,Country Davolio,Nancy,Sales Representative,USA Fuller,Andrew,Sales Manager,USA Leverling,Janet,Sales Representative,UK Suyama,Michael,Sales Representative,UK
  • 36. // Instantiate the reader on a CSV file XmlCsvReadWriter reader; reader = new XmlCsvReadWriter("employees.csv", hasHeader.Checked); reader.EnableOutput = true; reader.Read();
  • 37. // Define the schema of the table to bind to the grid DataTable dt = new DataTable(); for(int i=0; i<reader.AttributeCount; i++) { reader.MoveToAttribute(i); DataColumn col = new DataColumn(reader.Name, typeof(string)); dt.Columns.Add(col); }
  • 38. reader.MoveToElement(); // Loop through the CSV rows and populate the DataTable do { DataRow row = dt.NewRow(); for(int i=0; i<reader.AttributeCount; i++) {
  • 39. if (reader[i] == "Sales Representative") reader[i] = "Sales Force"; row[i] = reader[i].ToString(); } dt.Rows.Add(row); } while (reader.Read());
  • 40. // Flushes the changes to disk reader.Close(); // Bind the table to the grid dataGrid1.DataSource = dt;
  • 41. Readers and writers are at the foundation of every I/O operation in the .NET Framework. You find them at work when you operate on disk and on network files, when you serialize and deserialize, while you perform data access, even when you read and write configuration settings.
  • 42. XML writers are ad hoc tools for creating XML documents using a higherlevel metaphor and putting more abstraction between your code and the markup. By using XML writers, you go far beyond markup to reach a nodeoriented dimension in which, instead of just accumulating bytes in a block of contiguous memory, you assemble nodes and entities to create the desired schema and infoset
  • 43. .NET XML writers only ensure the well-formedness of each individual XML element being generated. Writers can in no way guarantee the well-formedness of the entire document and can do even less to validate a document against a DTD or a schema. Although badly formed XML documents can only result from actual gross programming errors, the need for an extra step of validation is often felt in production environments, especially when the creation of the document depends on a number of variable factors and run-time conditions. For this reason, we've also examined the key points involved in the design and implementation of a validating XML writer.