Sunday, December 25, 2011

Create an XML document using C# and looping variables

Here is an example of how to writing a looping argument for XML using the stream writer in Visual C# Basic.
Before you can run this code you will need to add a few namespaces.

using System.IO;
using System.Xml;
using System.Xml.Linq;

This program will explain how to write variables for a 3D structure's X,Y,Z coordinates to an XML file where they can later be read by a program that would need to read the numbers for each data point in the array.  This is a very specific requirement but perhaps it will give you ideas of how to create your own XML file using loops in C#. 

Below is an example of a method that can be called by an argument in your progrom. Using Stream writer to write looping arguments is the best method to ensure the XML file writes correctly. There are classes in C# that are designed to make XML files but I have found them unreliable for this type of use.


public void XStream()
{

       StreamWriter writer = new StreamWriter(@"C:\XStream.xml", true);
       writer.WriteLine("<?xml version=\"1.0\"?>");    
       writer.WriteLine("<DataPoints>");
       for (int p = 0; p < 9; x++)
       {
             writer.WriteLine("  <DataPoint id=\"" + x + z + "\">");
             writer.WriteLine("      <X>" + XPoint[p] + "</X>");
             writer.WriteLine("      <Y>" + YPoint[p] + "</Y>");
             writer.WriteLine("      <Z>" + ZPoint[p] + "</Z>");
             writer.WriteLine("  </DataPoint>");
       }
       writer.WriteLine("</DataPoints>");
       writer.Close();
}



If you open the XStream file in note pad the output should look similar to the code shown below, depending on what values were set for each XPoint, YPoint, and ZPoint object in the array:

<?xml version="1.0"?>
<DataPoints>
  <DataPoint id="0">
      <X>0</X>
      <Y>0</Y>
      <Z>1</Z>
  </DataPoint>
  <DataPoint id="1">
      <X>0.38268343236509</X>
      <Y>0.125</Y>
      <Z>0.923879532511287</Z>
  </DataPoint>
  <DataPoint id="2">
      <X>0.707106781186547</X>
      <Y>0.25</Y>
      <Z>0.707106781186548</Z>
  </DataPoint>
  <DataPoint id="3">
      <X>0.923879532511287</X>
      <Y>0.375</Y>
      <Z>0.38268343236509</Z>
  </DataPoint>
  <DataPoint id="4">
      <X>1</X>
      <Y>0.5</Y>
      <Z>6.12303176911189E-17</Z>
  </DataPoint>
  <DataPoint id="5">
      <X>0.923879532511287</X>
      <Y>0.625</Y>
      <Z>-0.38268343236509</Z>
  </DataPoint>
  <DataPoint id="6">
      <X>0.707106781186548</X>
      <Y>0.75</Y>
      <Z>-0.707106781186547</Z>
  </DataPoint>
  <DataPoint id="7">
      <X>0.38268343236509</X>
      <Y>0.875</Y>
      <Z>-0.923879532511287</Z>
  </DataPoint>
  <DataPoint id="8">
      <X>1.22460635382238E-16</X>
      <Y>1</Y>
      <Z>-1</Z>
  </DataPoint>
</DataPoints>



If you were to connect each data point in order, this program would draw a spiral.  The Code for the whole program is shown below, just add a button to your form in Visual C#.

using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double[] XPoint = new double[9];
        double[] YPoint = new double[9];
        double[] ZPoint = new double[9];

        private void button1_Click(object sender, EventArgs e)
        {
            for (int p = 0; p < 9; p++)
            {
                XPoint[p] = Math.Sin(Math.PI * p / 8);
                YPoint[p] = (double)p/8;
                ZPoint[p] = Math.Cos(Math.PI * p / 8);
            }
            XStream();
        }

        public void XStream()
        {
            StreamWriter writer = new StreamWriter(@"C:\Users\Delta\My Documents\XStream.xml", true);
            writer.WriteLine("<?xml version=\"1.0\"?>");
            writer.WriteLine("<DataPoints>");
            for (int p = 0; p < 9; p++)
            {
                writer.WriteLine("  <DataPoint id=\"" + p + "\">");
                writer.WriteLine("      <X>" + XPoint[p] + "</X>");
                writer.WriteLine("      <Y>" + YPoint[p] + "</Y>");
                writer.WriteLine("      <Z>" + ZPoint[p] + "</Z>");
                writer.WriteLine("  </DataPoint>");
            }
            writer.WriteLine("</DataPoints>");
            writer.Close();
        }
    }
}

No comments:

Post a Comment