Avatar of ITsolutionWizard
ITsolutionWizard
Flag for United States of America asked on

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>
XMLASP.NETC#

Avatar of undefined
Last Comment
YZlat

8/22/2022 - Mon
Lokesh B R

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

ITsolutionWizard

ASKER
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>
ITsolutionWizard

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Lokesh B R

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

ITsolutionWizard

ASKER
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 R

Hi,

Check my last post with for loop code.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
YZlat

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.