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?
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

0
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>
0
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

0
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>
0
Lokesh B RDeveloperCommented:
Hi,

Check my last post with for loop code.
0
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

0

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.