xml .net

I would like to create xml in .net / c# - using xmldocument like below.

<?xml version="1.0" encoding="utf-8"?>
<Quotes>
  <Quote>
      <Principal>
            <FirstName></FirstName>
      </Principal>
      <Indemnitor>
            <FirstName></FirstName>
      </Indemnitor>
  </Quote>
</Quotes>
LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Check this sample

  using (XmlWriter writer = XmlWriter.Create(@"D:\123.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Quotes");


                writer.WriteStartElement("Quote");

                writer.WriteStartElement("Principal");
                writer.WriteStartElement("FirstName");
                writer.WriteString("Scott");
                writer.WriteFullEndElement();

                writer.WriteStartElement("Indemnitor");
                writer.WriteStartElement("FirstName");
                writer.WriteString(null);
                writer.WriteFullEndElement();

                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

Open in new window

ITsolutionWizardAuthor Commented:
The output from your codes are below and it is wrong. Try to compare my original xml with yours

<?xml version="1.0" encoding="utf-8"?>
<Quotes>
  <Quote><Principal><FirstName>Scott</FirstName>
  <Indemnitor><FirstName></FirstName></Indemnitor>
  </Principal>
  </Quote>
</Quotes>
ITsolutionWizardAuthor Commented:
and if i want to add another one it is overwritten my first one.
I want to keep adding item to this xml document. not just overwrite.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Lokesh B RDeveloperCommented:
Hi,

You can use FOR / FOREACH loop as below to create number of repeating items in the XML file.


  using (XmlWriter writer = XmlWriter.Create(@"D:\123.xml"))
 {
                writer.WriteStartDocument();
                writer.WriteStartElement("Quotes");

                for (int i = 0; i < 2; i++)
                {
                    writer.WriteStartElement("Quote");

                    writer.WriteStartElement("Principal");
                    writer.WriteStartElement("FirstName");
                    writer.WriteString("Scott" + i);
                    writer.WriteFullEndElement();

                    writer.WriteStartElement("Indemnitor");
                    writer.WriteStartElement("FirstName");
                    writer.WriteString(null);
                    writer.WriteFullEndElement();

                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

Open in new window

ITsolutionWizardAuthor Commented:
The output from your codes are below and it is wrong. Try to compare my original xml with yours

<?xml version="1.0" encoding="utf-8"?>
<Quotes>
  <Quote><Principal><FirstName>Scott</FirstName>
  <Indemnitor><FirstName></FirstName></Indemnitor>
  </Principal>
  </Quote>
</Quotes>
Lokesh B RDeveloperCommented:
Hi,

Check my last post with for loop code.
YZlatCommented:
Try this:

using (XmlWriter writer = XmlWriter.Create(@"D:\123.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Quotes");

                    writer.WriteStartElement("Quote");

                    writer.WriteStartElement("Principal");
                    writer.WriteElementString("FirstName","a");
                    writer.WriteEndElement();

                    writer.WriteStartElement("Indemnitor");
                    writer.WriteElementString("FirstName","b");
                    writer.WriteEndElement();

                    writer.WriteEndElement();


                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.