View Single Post
  #7 (permalink)  
Old January 7th, 2008, 05:44 PM
Sojan80 Sojan80 is offline
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, never mind... I got ti working fine. It turns out I was missing an if statement.
This code now works a treat:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using AISUtilLib;
using System.Data.OracleClient;

namespace XmlTest3
{
    class Program
    {
        static void Main(string[] args)
        {

            // Declare some variables to hold values for processing
            String fileDate = String.Empty;
            String savedApplicationId = String.Empty;
            String eTagName = String.Empty;
            String eValue = String.Empty;
            String eUserName = "XmlParser";

            // Set nodeAttribute to 0 by default
            // This means there were no attributes found
            String eTagId = "0";

            string xmlPath = @"DataFile.xml";
            //string xmlPath = @"DataFile2.xml";
            XmlDocument doc = new XmlDocument();

            try
            {
                // Load the Xml from the file
                doc.PreserveWhitespace = false;
                doc.Load(xmlPath);

                // Get the file Date before we do anything else
                fileDate = doc.SelectSingleNode("//FileDate").InnerText.ToString();
                //Console.WriteLine("File Date: {0}", fileDate);

                // Get all the Application nodes in the file by using SelectNodes
                foreach (XmlNode appNode in doc.SelectNodes("//Application"))
                {
                    /* At this point we should have a collection of all the
                     * application nodes in the file. We also know that
                     * Application has a ChildNodes collection associated with
                     * it. */

                    // Get the savedApplicationId for this Application node
                    savedApplicationId = appNode.SelectSingleNode("//SavedApplicationID").InnerText.ToString();

                    // Now we need to loop through all the child nodes of Application
                    foreach (XmlNode childNode in appNode.ChildNodes)
                    {
                        // This will test to see that the local name of childNode
                        // is not one we want to exclude / skip.

                        switch (childNode.LocalName)
                        {
                            case "UniqueHash": // We want to skip over this field entirely
                                break;
                            case "ACTIVITY_STATEMENT": // We want to skip over this field entirely
                                break;
                            case "ENRICHMENT_STATEMENT": // We want to skip over this field entirely
                                break;
                            case "STATEMENT": // We want to skip over this field entirely
                                break;
                            default:
                                // Get the attributes collection for the Xml Node in question
                                XmlAttributeCollection attrs = childNode.Attributes;
                                if ((attrs.Count > 0))
                                {
                                    // We have an ID attribute, process accordingly
                                    foreach (XmlAttribute attr in attrs)
                                    {
                                        // We have an attribute and assign the value
                                        eTagId = attr.Value.ToString();

                                        // We need to do a for each for each of the child nodes
                                        // returning tag name, value and calling save
                                        foreach (XmlNode node in childNode.ChildNodes)
                                        {
                                            eTagName = node.LocalName.ToString();
                                            eValue = node.InnerText.ToString();
                                            Save(fileDate, savedApplicationId, eTagName, eTagId, eValue, eUserName);
                                        }

                                    }

                                }
                                else
                                {
                                    // We don't have an ID attribute
                                    eTagName = childNode.LocalName.ToString();
                                    eValue = childNode.InnerText.ToString();
                                    Save(fileDate, savedApplicationId, eTagName, eTagId, eValue, eUserName);

                                }
                                break;

                        }


                    }

                }
                Console.ReadLine();

            }
            catch (XmlException xmlEx)
            {
                Console.Write("Xml Exception: " + xmlEx.Message);
            }
            catch (Exception ex)
            {
                Console.Write("Exception: " + ex.Message);
            }

        }// end static void Main
        public static void Save(string fileDate, string savedApplicationId, string eTagName, string eTagId, string eValue, string eUserName)
        {
            String eAppSql = String.Empty;
            //eAppSql = "INSERT INTO " ;
            eAppSql = "File Date: " + fileDate.ToString() + " Saved App Id: " + savedApplicationId.ToString() + " Tag Name: " + eTagName.ToString() + " Tag Id: " + eTagId.ToString() + " Value: " + eValue.ToString();
            Console.WriteLine(eAppSql);
            Console.WriteLine();


            // Establish Connection to Oracle Datastore
            //OracleConnection oConn = new OracleConnection(AISUtilLib.AISConnStrOra._f_SIS_U WSPOL_PUB("Test"));

            //eAppSql = "INSERT INTO EAPP_DTL ('EAPP_FILE_DT','EAPP_APP_ID','EAPP_TAG','EAPP_TAG _ID','EAPP_VALUE','EAPP_TIMESTAMP_TMS',EAPP_USER_N M') VALUES ('" + Convert.ToDateTime(fileDate.ToString()) + "','" + savedApplicationId.Trim().ToString() + "','" + eTagName.Trim().ToString() + "','" + eTagId.Trim().ToString() + "','" + eValue.Trim().ToString() + "','" + SYSDATE + "','" + eUserName.Trim().ToString() + "'";

            //AISUtilLib.AISOracle._f_ExecNonQuery(eAppSql, oConn);

        } //end public static void Save



    }
}


Is there a way to tighten this up a bit more or make it better ?

Reply With Quote