none
CONVERT RRS feed

  • Question

  • HI

    IM A STUDENT THAT HAVE AN EMERGENCY PROJECT THAT SHOULD CONVERT STRING INTO 5000 XML FILE AND HAVE THIS CODE AND NEED TO HELP FOR COMPLETE THIS MISSION

    THANK YOU VERY MUCH <3 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using WindowsFormsApplication35.Model;

    namespace WindowsFormsApplication35
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            private void Form2_Load(object sender, EventArgs e)
            {
                string dc = "";
                string dwc = "";
                string foxml = "";
                
                // Record 1
                Objects o = new Objects();
                o.subject = "GUI";
                o.title = "this is sample record1";

                dc = CreateDCRecord(o);
                dwc = CreateDWCRecord(o);

                foxml = CreateFOXMLRecord(dwc,dc);

                WriteAllFile(foxml,"1");

                //Record 2
                Objects o2 = new Objects();
                o2.subject = "GUI";
                o2.title = "this is sample record1";

                dc = CreateDCRecord(o2);
                dwc = CreateDWCRecord(o2);

                foxml = CreateFOXMLRecord(dwc, dc);

                WriteAllFile(foxml, "2");

                //Record 3
                Objects o3 = new Objects();
                o3.subject = "GUI";
                o3.title = "this is sample record1";

                dc = CreateDCRecord(o3);
                dwc = CreateDWCRecord(o3);

                foxml = CreateFOXMLRecord(dwc, dc);

                WriteAllFile(foxml, "3");
            }

            private void WriteAllFile(string FileContent, string FileName)
            {
                System.IO.File.WriteAllText(@"C:\Temp\FOXML"+FileName+".xml", FileContent);
            }

            private static string CreateDCRecord(Objects o)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sb.Append("<Items>");
                sb.Append("<item>");
                sb.Append("<title>" + o.title + "</title>");
                sb.Append("<subject>" + o.subject + "</subject>");
                sb.Append("</item>");
                sb.Append("</Items>");
                return sb.ToString();
            }

            private static string CreateDWCRecord(Objects o)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sb.Append("<Items>");
                sb.Append("<item>");
                sb.Append("<title>" + o.title + "</title>");
                sb.Append("<subject>" + o.subject + "</subject>");
                sb.Append("</item>");
                sb.Append("</Items>");
                return sb.ToString();
            }

            private static string CreateFOXMLRecord(string dwc, string dc)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sb.Append("<Items>");
                sb.Append("<item>");
                sb.Append("<dwc>" + dwc + "</dwc>");
                sb.Append("<dc>" + dc + "</dc>");
                sb.Append("</item>");
                sb.Append("</Items>");
                return sb.ToString();
            }

            private void button1_Click(object sender, EventArgs e)
            {

            }
        }
    }

    Monday, August 3, 2015 7:12 PM

Answers

  • What specifically do you need help with?  Looks like you're doing ok so far.

    If you just want general advice:

    • Don't build XML by hand.  for example, consider what might happen if the title of your Objects was the string "How to write </title> to a file". This would result in the following text "<title>How to write </title> to a file</title>" which should be obvious what is wrong with that.  You need to escape certain characters and represent them with their entities, like < with &lt;  You can use XDocument or XmlDocument, or XmlTextWriter.
    • You seem to have hand-rolled (hard coded) a number of records by repeating chunks of very similar code.  This is generally error prone when it comes to maintenance of the code.  You should make use of more functions at a more granular level and pass parameters.  Generalize your code to work in more situations with the use of variables and parameters.
    • Your code seems to exist in a Windows Form class.  Writing Objects to an XML file is has nothing to do with a windows form.  You should write a new class for the purposes of converting your objects to XML, or extend the Objects class.
    • XML Serialization in C# is largely taken care of for you by the framework via a number of supporting classes including XmlSerializer and, some associated attributes. The implementation can discover the structure of your objects automatically via reflection.  See How to: Serialize an Object.  
    Monday, August 3, 2015 9:44 PM