Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8025727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:15:31+00:00 2026-06-04T23:15:31+00:00

The Usual way, i code to write a XML file is, XmlWriterSettings settings =

  • 0

The Usual way, i code to write a XML file is,

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    XmlWriter writer = XmlWriter.Create("Products.xml", settings);

    writer.WriteStartDocument();

    writer.WriteComment("This file is generated by the program.");

    writer.WriteStartElement("Product");
    writer.WriteAttributeString("ID", "001");
    writer.WriteAttributeString("Name", "Keyboard");
    writer.WriteElementString("Price", "10.00");
    writer.WriteStartElement("OtherDetails");
    writer.WriteElementString("BrandName", "X Keyboard");
    writer.WriteElementString("Manufacturer", "X Company");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();

But the above code gives me a different XML structure, How to code if i need the output as below given structure,

<Books>
<Book ISBN="0553212419">
<title>Sherlock Holmes</title>
<author>Sir Arthur Conan Doyle</author>
</Book>
<Book ISBN="0743273567">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
</Book>
<Book ISBN="0684826976">
<title>Undaunted Courage</title>
<author>Stephen E. Ambrose</author>
</Book>
<Book ISBN="0743203178">
<title>Nothing Like It In the World</title>
<author>Stephen E. Ambrose</author>
</Book>
</Books>

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:15:32+00:00Added an answer on June 4, 2026 at 11:15 pm

    As was commented, just modify the code you already have to write the correct elements.

    XmlWriter writer = XmlWriter.Create(@"Products.xml", settings);
    
    writer.WriteStartDocument();
    
    writer.WriteComment("This file is generated by the program.");
    
    writer.WriteStartElement("Books");
    writer.WriteStartElement("Book");
    writer.WriteAttributeString("ISBN", "0553212419");
    writer.WriteElementString("Title", "Sherlock Holmes");
    writer.WriteElementString("Author", "Sir Arthur Conan Doyle");
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();
    

    Wash, rinse, repeat. I would recommend writing a method to add each book.

    EDIT – method for writing books

    void WriteBookData(XmlWriter writer, string isbn, string title, string author)
    {
        writer.WriteStartElement("Book");
        writer.WriteAttributeString("ISBN", isbn);
        writer.WriteElementString("Title", title);
        writer.WriteElementString("Author", author);
        writer.WriteEndElement();
    }
    
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(@"Products.xml", settings))
    {
        writer.WriteStartDocument();
    
        writer.WriteComment("This file is generated by the program.");
    
        writer.WriteStartElement("Books");
        WriteBookData(writer, "0553212419", "Sherlock Holmes", "Sir Arthur Conan Doyle");
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the usual/clearest way to write this in Python? value, _ = func_returning_a_tuple()
The usual way to read a file in C++ is this one: std::ifstream file(file.txt,
The bellow code working, but i don’t know it the write way or not?
The usual way to resolve lnk involve using WShell.WshShortcut or IShellLink that way :
The usual way to assign color box functionality on a link is like this:
A usual way to put, for example a menu tab with background is as
What is the most usual way to develop views in asp.net mvc? Should I
This is my usual way to debug javascript. Include alert(0); to break the flow
As I remember and checked, the usual way for traversing a tree or crawling
I want to comment several lines in my .vimrc, the usual way I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.