xml .net

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
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>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lokesh B RDeveloper
Top Expert 2015

Commented:
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

Author

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>

Author

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.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Lokesh B RDeveloper
Top Expert 2015

Commented:
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

Author

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 RDeveloper
Top Expert 2015

Commented:
Hi,

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial