We help IT Professionals succeed at work.

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

Bruce
Bruce asked
on
399 Views
Last Modified: 2013-12-16
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!
Comment
Watch Question

Carlos VillegasFull Stack .NET Developer

Commented:
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

Carlos VillegasFull Stack .NET Developer

Commented:
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

Carlos VillegasFull Stack .NET Developer

Commented:
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

Carlos VillegasFull Stack .NET Developer

Commented:
Sorry if I mistyped words, my english is not very well...
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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
Carlos VillegasFull Stack .NET Developer

Commented:
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

Full Stack .NET Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.