[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Need high level steps to transform flat file to XML doc IN C#

I am a relatively experience ASP.NET C# programmer but have not had much call to work with XML documents.  On my current project I have the need to transform a simple flat file extraction from a mainframe to an XML doc.  

I would like a relatively high level explanation of the steps to perform this task.  That being said ("high-level"), please feel free to provide as much detail as you think necessary to cover the subject.

Thanks!
0
Bruce
Asked:
Bruce
  • 6
  • 2
3 Solutions
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Got the idea?
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            
System.Xml.XmlElement rootNode = doc.CreateElement("Root");
doc.AppendChild(rootNode);

System.Xml.XmlElement rootSubNode = doc.CreateElement("SubNode");
System.Xml.XmlAttribute nodeAttribute = doc.CreateAttribute("A1");
nodeAttribute.Value = "test";
rootSubNode.Attributes.Append(nodeAttribute);
rootNode.AppendChild(rootSubNode);

System.Xml.XmlElement rootSubNode2 = doc.CreateElement("SubNode");
System.Xml.XmlAttribute nodeAttribute2 = doc.CreateAttribute("A2");
nodeAttribute2.Value = "test 2";
rootSubNode2.Attributes.Append(nodeAttribute2);
rootSubNode.AppendChild(rootSubNode2);

StringBuilder sbXml = new StringBuilder();
System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
writerSettings.ConformanceLevel = System.Xml.ConformanceLevel.Document;
writerSettings.Indent = true;
using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sbXml, writerSettings))
{
    doc.Save(xmlWriter);
}

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Another way, better in fly:
StringBuilder sbXml = new StringBuilder();
System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
writerSettings.ConformanceLevel = System.Xml.ConformanceLevel.Document;
writerSettings.Indent = true;
using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sbXml, writerSettings))
{
    xmlWriter.WriteStartElement("Root");

    xmlWriter.WriteStartElement("SubNode");
    xmlWriter.WriteAttributeString("A1", "test");

    xmlWriter.WriteStartElement("SubNode2");
    xmlWriter.WriteAttributeString("A2", "test 2");
    xmlWriter.WriteEndElement();

    xmlWriter.WriteEndElement();

    xmlWriter.WriteEndElement();
}

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Better explained:
StringBuilder sbXml = new StringBuilder();
            
System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
writerSettings.ConformanceLevel = System.Xml.ConformanceLevel.Document;
writerSettings.Indent = true;

using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sbXml, writerSettings))
{
    // Create root node
    xmlWriter.WriteStartElement("Root");

    // Create a root child
    xmlWriter.WriteStartElement("SubNode");
    // Write an attribute to SubNode
    xmlWriter.WriteAttributeString("A1", "test");
    // Write text inside SubNode
    xmlWriter.WriteValue("HELLO!");

    // Create a child of SubNode2
    xmlWriter.WriteStartElement("SubNode2");
    // Write an attribute to SubNode2
    xmlWriter.WriteAttributeString("A2", "test 2");
    // Write a date inside SubNode2
    xmlWriter.WriteValue(DateTime.Now);
    xmlWriter.WriteEndElement();// Close SubNode2

    // Close SubNode
    xmlWriter.WriteEndElement();

    // Close root
    xmlWriter.WriteEndElement();
}

// Your Xml document data
Console.WriteLine(sbXml.ToString());
}

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
Carlos VillegasFull Stack .NET DeveloperCommented:
Sorry if I mistyped words, my english is not very well...
0
 
Fernando SotoCommented:
Hi cylikon;

Well the first thing you need to know is what the structure of the document will be.
What version of .Net are you using?

Fernando
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Better explained yet:
StringBuilder sbXml = new StringBuilder();

System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
writerSettings.ConformanceLevel = System.Xml.ConformanceLevel.Document;
writerSettings.Indent = true;

using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sbXml, writerSettings))
{
    // Create root node
    xmlWriter.WriteStartElement("Root");

    // To simulate yours text lines.
    for (int i = 0; i < 3; i++)
    {
        // Create a root child
        xmlWriter.WriteStartElement("SubNode");

        // Write an attribute to SubNode
        xmlWriter.WriteStartAttribute("A1");
        xmlWriter.WriteValue("test " + i);
        xmlWriter.WriteEndAttribute();

        // Write text inside SubNode
        xmlWriter.WriteValue("HELLO! " + i);

        // Create a child of SubNode
        xmlWriter.WriteStartElement("SubNode2");

        // Write an attribute to SubNode2
        xmlWriter.WriteStartAttribute("A2");
        xmlWriter.WriteValue("test " + i);
        xmlWriter.WriteEndAttribute();

        // Write a date inside SubNode2
        xmlWriter.WriteValue(DateTime.Now);
        // Close SubNode2
        xmlWriter.WriteEndElement();

        // Close SubNode
        xmlWriter.WriteEndElement();
    }

    // Close root
    xmlWriter.WriteEndElement();
}

// Your Xml document data
Console.WriteLine(sbXml.ToString());

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Well, here you have a better conversion example based on your scenario:
// Simulate your MainFrame text plain file.
StringBuilder sbPlainFile = new StringBuilder();
using (System.IO.StringWriter sw = new System.IO.StringWriter(sbPlainFile))
{
    Random rnd = new Random();
    for (int i = 0; i < 100; i++)
    {
        // Add dummy data.
        sw.Write(DateTime.Now.AddDays(i));
        sw.Write('\t');
        sw.Write(i);
        sw.Write('\t');
        sw.Write(rnd.NextDouble() * 1000);
        sw.WriteLine();
    }
}

// Simulate read the lines in your file. 
using (System.IO.StringReader sw = new System.IO.StringReader(sbPlainFile.ToString()))
{
    // Simulate saving the xml data to a filestream
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        // Set your basics xml settigns.
        System.Xml.XmlWriterSettings writerSettings = new System.Xml.XmlWriterSettings();
        writerSettings.ConformanceLevel = System.Xml.ConformanceLevel.Document;
        writerSettings.Indent = true;
        writerSettings.Encoding = Encoding.UTF8;

        using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(ms, writerSettings))
        {
            // Create a root node
            xmlWriter.WriteStartElement("PlainText");

            // Read the lines...
            string textLine = sw.ReadLine();
            while (textLine != null)
            {
                object[] lineValues = textLine.Split('\t');

                // Write Line element
                xmlWriter.WriteStartElement("Line");

                // Write Date element
                xmlWriter.WriteStartElement("Date");
                // When use WriteValue is important to pass the correct type, for the writer can make the right conversion.
                xmlWriter.WriteValue(Convert.ToDateTime(lineValues[0]));
                // Close Date element
                xmlWriter.WriteEndElement();

                // Write ID element
                xmlWriter.WriteStartElement("ID");
                xmlWriter.WriteValue(Convert.ToInt32(lineValues[1]));
                // Close ID element
                xmlWriter.WriteEndElement();

                // Write Value element
                xmlWriter.WriteStartElement("Value");
                xmlWriter.WriteValue(Convert.ToDouble(lineValues[2]));
                // Close Value element
                xmlWriter.WriteEndElement();

                // Close Line element
                xmlWriter.WriteEndElement();

                // Read next text line.
                textLine = sw.ReadLine();
            }

            // Close root node.
            xmlWriter.WriteEndElement();
        }

        ms.Flush();

        // Convert the stream to text.
        string myXml = System.Text.Encoding.UTF8.GetString(ms.ToArray());

        // Send to the console
        Console.WriteLine(myXml);
    }
}

Open in new window


I hope this help.
0
 
vishal18_guptaCommented:
1) Read text file
2) Look into text file to identify xml text you want to load in XmlDocument
3) Split text using Regex
4) Load the xmlText into XmlDocument.
5) Query xml using LInq/Xpath
0
 
Fernando SotoCommented:
If you are using Visual Studio 2008 SP1 you can use Linq to XML directly to get the needed information.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now