Tranforming .txt to XML output issue

Getting inccrrect output when tranforming .txt input to xml output

Page 1 of 1

0 Replies - 593 Views - Last Post: 06 May 2010 - 01:45 PM Rate Topic: -----

#1 KeepsHunger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-May 10

Tranforming .txt to XML output issue

Posted 06 May 2010 - 01:45 PM

Hi All -

I was wondering if someone could point me in the right directions as to why I am getting some funky output when transforming .txt input to xml.

I have a program which reads in a .txt file that has data delimited by ; and each line by | and DTD file. The program then outputs xml conforming to the DTD. For designated xml elements the program outputs the contenets of the .txt file.

For example

Input .txt:
A1;A2;A3|
B1;B2;B3|

Code snippet which outputs Xml:
if (strucHash.ContainsKey(child_name + num) && !child_name.Equals(""))
                        {
                            // If element is in most element 
                            if (child_name.Equals("row") || child_name.Equals("entry"))
                            {

                                for (int x = 0; x < row.Length; x++)
                                {

                                    

                                    if (child_name.Equals("row"))
                                    {
                                        // get the element
                                        XmlElement elmRoot; //= doc.DocumentElement;

                                        // create a new element for current "child"
                                        XmlElement elmNew = doc.CreateElement(child_name);

                                        // find cuurent element
                                        elmRoot = doc.DocumentElement;

                                        // create new element
                                        elmNew = doc.CreateElement(child_name);

                                        // append element to file
                                        elmRoot.FirstChild.FirstChild.AppendChild(elmNew);

                                    }


                                    // If element is in most element
                                    if (child_name.Equals("entry"))
                                    {
                                        // get the element
                                        XmlElement elmRoot = doc.DocumentElement;

                                        // create a new element for current "child"
                                        XmlElement elmNew = doc.CreateElement(child_name);

                                        // set the .txt delimiter
                                        string valdelim = ";";
                                        char[] valdelimiter = valdelim.ToCharArray();

                                     


                                            // put each value into an array
                                            string[] NewValue = row[x].Split(valdelimiter);

                                            Console.WriteLine("ROW NUMBER = ");
                                           // Console.WriteLine(x);

                                            // for each value
                                            for (int a = 0; a < NewValue.Length; a++)
                                            {


                                                // find cuurent element
                                                elmRoot = doc.DocumentElement;

                                                // create new element
                                                elmNew = doc.CreateElement(child_name);

                                                // create text node
                                                XmlText txtValue = doc.CreateTextNode(NewValue[a]);

                                               // append element to file
                                             elmRoot.LastChild.LastChild.LastChild.AppendChild(elmNew);

                                               // append new element text to file
                                                elmRoot.LastChild.LastChild.LastChild.LastChild.AppendChild(txtValue);



                                            
                                            }
                                      
                                    }
                                    else
                                    {
                                        CreateXMLDoc(child, (child_name + num), row);
                                    }

                                   }
                            }
                            else
                            {
                                CreateXMLDoc(child, (child_name + num), row);
                            }


Output:

<row>
<entry />
<entry />
</row>
<row>
<entry >A1</entry>
<entry >A2</entry>
<entry >A3</entry>
<entry />
<entry >B1</entry>
<entry >B2</entry>
<entry >B3</entry>
</row>

Output Needed:

<row>
<entry >A1</entry>
<entry >A2</entry>
<entry >A3</entry>
</row>
<row>
<entry >B1</entry>
<entry >B2</entry>
<entry >B3</entry>
</row>

Any help would be much appreciated.

Thanks,
KeepsHunger

Is This A Good Question/Topic? 0
  • +

Page 1 of 1