SlideShare a Scribd company logo
1 of 11
Techniques for Writing XML Data
Writing XML with the XmlWriter Class
• XmlWriterSettings.CheckCharacters property
configures the XmlWriter to check for illegal
characters in text nodes and XML names, as well as
check the validity of XML names . An exception is
thrown if an invalid character is detected .
• XmlWriterSettings.ConformanceLevel property
configures the XmlWriter to guarantee that the
stream complies with the conformance level that is
specified. For example, the XML may be set to
conform to a document or document fragment.
• XmlWriter. WriteValue method is used to write data
to the XML stream as a CLR type (int , double , and so
on) without having to first convert it to a string.
Write XML Using XmlWriter Class
private void WriteMovie()
{ string[,] movieList = { { "Annie Hall", "Woody Allen" }, {
"Lawrence of Arabia", "David Lean" } };
// (1) Define settings to govern writer actions
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; settings.IndentChars = (" ");
settings.ConformanceLevel =
ConformanceLevel.Document;
settings.CloseOutput = false;
settings.OmitXmlDeclaration = false;
Write XML Using XmlWriter Class
private void WriteMovie()
{
string[,] movieList = { { "Annie Hall", "Woody Allen" }, { "Lawrence of
Arabia", "David Lean" } };
// (1) Define settings to govern writer actions XmlWriterSettings settings
= new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
settings.ConformanceLevel =ConformanceLevel.Document;
settings.CloseOutput = false;
settings.OmitXmlDeclaration = false;
Write XML Using XmlWriter Class
// (2) Create XmlWriter object XmlWriter writer =
XmlWriter.Create("c:mymovies.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("Output from xmlwriter class");
writer.WriteStartElement("films");
for (int i = 0; i <= movieList.GetUpperBound(0) ; i++)
{
try
{ writer.WriteStartElement("movie");
writer.WriteElementString("Title", movieList[i, 0]);
writer.WriteElementString("Director", movieList[i, 1]);
writer.WriteStartElement("Movie_ID");
writer.WriteValue(i); // No need to convert to string
writer.WriteEndElement();
writer.WriteEndElement();}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
writer.WriteEndElement();
writer.Flush();
// Flush any remaining content to XML stream
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
writer.WriteEndElement();
writer.Flush();
// Flush any remaining content to XML stream
writer.Close();
}
<?xml version="1.0" encoding="utf-8"?>
<!--Output from xmlwriter class-->
<films>
<movie>
<Title>Annie Hall</Title>
<Director>Woody Allen</Director>
<Movie_ID>0</Movie_ID>
</movie>
<movie>
<Title>Lawrence of Arabia</Title>
<Director>David Lean</Director>
<Movie_ID>1</Movie_ID>
</movie>
</films>
• .NET also provides XmlTextWriter and
XmlNodeWriter classes as concrete
implementations of the abstract XmlWriter
class
• node writer creates a DOM tree in memory
that can be processed using the many classes
and methods designed for that task.
Using XPath to Search XML
Techniques for Writing XML Data.pptx

More Related Content

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Techniques for Writing XML Data.pptx

  • 2. Writing XML with the XmlWriter Class • XmlWriterSettings.CheckCharacters property configures the XmlWriter to check for illegal characters in text nodes and XML names, as well as check the validity of XML names . An exception is thrown if an invalid character is detected . • XmlWriterSettings.ConformanceLevel property configures the XmlWriter to guarantee that the stream complies with the conformance level that is specified. For example, the XML may be set to conform to a document or document fragment. • XmlWriter. WriteValue method is used to write data to the XML stream as a CLR type (int , double , and so on) without having to first convert it to a string.
  • 3. Write XML Using XmlWriter Class private void WriteMovie() { string[,] movieList = { { "Annie Hall", "Woody Allen" }, { "Lawrence of Arabia", "David Lean" } }; // (1) Define settings to govern writer actions XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); settings.ConformanceLevel = ConformanceLevel.Document; settings.CloseOutput = false; settings.OmitXmlDeclaration = false;
  • 4. Write XML Using XmlWriter Class private void WriteMovie() { string[,] movieList = { { "Annie Hall", "Woody Allen" }, { "Lawrence of Arabia", "David Lean" } }; // (1) Define settings to govern writer actions XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); settings.ConformanceLevel =ConformanceLevel.Document; settings.CloseOutput = false; settings.OmitXmlDeclaration = false;
  • 5. Write XML Using XmlWriter Class // (2) Create XmlWriter object XmlWriter writer = XmlWriter.Create("c:mymovies.xml", settings); writer.WriteStartDocument(); writer.WriteComment("Output from xmlwriter class"); writer.WriteStartElement("films"); for (int i = 0; i <= movieList.GetUpperBound(0) ; i++) { try { writer.WriteStartElement("movie"); writer.WriteElementString("Title", movieList[i, 0]); writer.WriteElementString("Director", movieList[i, 1]); writer.WriteStartElement("Movie_ID"); writer.WriteValue(i); // No need to convert to string writer.WriteEndElement(); writer.WriteEndElement();}
  • 8. <?xml version="1.0" encoding="utf-8"?> <!--Output from xmlwriter class--> <films> <movie> <Title>Annie Hall</Title> <Director>Woody Allen</Director> <Movie_ID>0</Movie_ID> </movie> <movie> <Title>Lawrence of Arabia</Title> <Director>David Lean</Director> <Movie_ID>1</Movie_ID> </movie> </films>
  • 9. • .NET also provides XmlTextWriter and XmlNodeWriter classes as concrete implementations of the abstract XmlWriter class • node writer creates a DOM tree in memory that can be processed using the many classes and methods designed for that task.
  • 10. Using XPath to Search XML