XML Sub Elements

Hi,

I'm trying learn more about creating XML documents.  I'm using this example:

using System;
using System.Data;
using System.Windows.Forms;
using System.Xml; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("Table");
            createNode("1", "Product 1", "1000", writer);
            createNode("2", "Product 2", "2000", writer);
            createNode("3", "Product 3", "3000", writer);
            createNode("4", "Product 4", "4000", writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MessageBox.Show("XML File created ! ");
        }

        private void createNode(string pID, string pName, string pPrice, XmlTextWriter writer)
        {
            writer.WriteStartElement("Product");
            writer.WriteStartElement("Product_id");
            writer.WriteString(pID);
            writer.WriteEndElement();
            writer.WriteStartElement("Product_name");
            writer.WriteString(pName);
            writer.WriteEndElement();
            writer.WriteStartElement("Product_price");
            writer.WriteString(pPrice);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}

Open in new window


this produces this:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <PhysicalItem>
- <Product>
  <Product_id>1</Product_id> 
  <Product_name>Product 1</Product_name> 
  <Product_price>1000</Product_price> 
  </Product>
- <Product>
  <Product_id>2</Product_id> 
  <Product_name>Product 2</Product_name> 
  <Product_price>2000</Product_price> 
  </Product>
- <Product>
  <Product_id>3</Product_id> 
  <Product_name>Product 3</Product_name> 
  <Product_price>3000</Product_price> 
  </Product>
- <Product>
  <Product_id>4</Product_id> 
  <Product_name>Product 4</Product_name> 
  <Product_price>4000</Product_price> 
  </Product>
  </PhysicalItem>

Open in new window


I can't seem to work out how I would create sub elements so that the end result would this:

 <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <PhysicalItem>
- <Product>
  <Product_id>1</Product_id> 
  <Product_name>Product 1</Product_name> 
  <Product_price>1000</Product_price>
<Objects>
<test>
<data>value01</data>
</test>
<test>
<data>value02</data>
</test>
</Objects>
  </Product>
-  </PhysicalItem>

Open in new window



Any suggestions?
andyw27Asked:
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.

käµfm³d 👽Commented:
Do you need to use the XmlTextWriter? I think you would have an easier time accomplishing this with the XmlDocument or XDocument classes.
0
Easwaran ParamasivamCommented:
Use XDocument and create XML document easily. Below is sample code. Please modify as per your need. For detailed information please do refer http://msdn.microsoft.com/en-us/library/bb308960.aspx

XDocument doc = new XDocument(
				new XDeclaration("1.0", "gb2312", string.Empty),
				new XElement( "products",
					new XElement( "product",
						new XAttribute( "id", "01" ),
						new XElement( "Name", "Java" ),
						new XElement( "Price", "Free" ) ),
					new XElement( "product",
						new XAttribute( "id", "02" ),
						new XElement( "Name", "C#" ),
						new XElement( "Price", "Free" )
						) ) );

Open in new window


You can use XmlDocument as well. Please do refer http://www.java2s.com/Code/CSharp/XML/ProgrammaticallycreatinganewXMLdocument.htm.

I prefer XDocument over the XMLDocument.
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
andyw27Author Commented:
Thanks, I managed to get it working using this:

               XmlNode Number = doc.CreateElement("Number");
                Item.AppendChild(Number).InnerText = "Text Value";
0
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
C#

From novice to tech pro — start learning today.