-2

I have a string like this { {Name Mike} {age 19} {gender male}} in a txt file. I would like this to be converted to xml as the below output. As i am new to this, it seems to be pretty doubts for me.

<name>Mike</name>
<age>19</age>
<gender>male</male>

any help would be appreciated.

stackdoubt
  • 11
  • 5
  • 3
    That's not any common markup, though it is somewhat close to json. You'll have to parse it yourself and build up your xml document. – Jonesopolis Apr 13 '16 at 00:30
  • You could use something like this "http://stackoverflow.com/questions/7771500/converting-array-of-objects-to-xml-in-c-sharp" – Firoz Apr 13 '16 at 00:49
  • Thanks. But i would like the "Name" become the attribute and "Mike" should be the value for it. – stackdoubt Apr 13 '16 at 01:02

1 Answers1

0

Here is my solution, at first you have to create a xml file in my case I have created x.xml at my bin folder and must create a root elemnt on the xml file, in my case sample xml at the begening as below, root element name can be anything, I have used just root

<root>
</root>

then code for writting you string as below

            string s = "{{Name Mike} {age 19} {gender male}}";
            string[] s2 = s.Replace("{", "").Replace("}", "").Split(' ');
            for (int i = 0; i < s2.Length; i++)
            {

                XDocument doc = XDocument.Load("x.xml");
                XElement rt = doc.Element("root");
                XElement elm = rt.Element(s2[i]);

                if (elm != null)
                {
                    elm.SetValue(s2[i + 1]);
                }
                else
                {
                    XElement x = new XElement(s2[i], s2[i + 1]);
                    rt.Add(x);
                }

                doc.Save("x.xml");
                i++;
            }

hope this will solve your problem

Update

if you want to automate file creation without creating the xml file by hand then you can do this way

            string s = "{{Name Mike} {age 19} {gender male}}";
            string[] s2 = s.Replace("{", "").Replace("}", "").Split(' ');

            if (!File.Exists("x.xml"))
            {
                TextWriter tw = new StreamWriter("x.xml", true);
                tw.WriteLine("<root>\n</root>");
                tw.Close();
            }

            for (int i = 0; i < s2.Length; i++)
            {

                XDocument doc = XDocument.Load("x.xml");
                XElement rt = doc.Element("root");
                XElement elm = rt.Element(s2[i]);

                if (elm != null)
                {
                    elm.SetValue(s2[i + 1]);
                }
                else
                {
                    XElement x = new XElement(s2[i], s2[i + 1]);
                    rt.Add(x);
                }

                doc.Save("x.xml");
                i++;
            }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • Instead of manually creating an xml file and is there a way that it can be automated since this needs to be created as an installer file. – stackdoubt Apr 13 '16 at 10:57
  • please see my answer update for that, the xml file will be created automatically if not present – Mostafiz Apr 13 '16 at 11:17
  • actually i am doing like this string s = @"E:\Newfolder\sample.txt"; but it hits an exception on XElement elm = rt.Element(s2[i]); "The ':' character, hexadecimal value 0x3A, cannot be included in a name." – stackdoubt Apr 13 '16 at 13:06
  • ok in your string there is a extra space at begin `{ {Name Mike} {age 19} {gender male}}` after first bracket but my one not `{{Name Mike} {age 19} {gender male}}` problem is there, is it possible to format string as a consitence way like mine ? if not then you have to ignore that – Mostafiz Apr 13 '16 at 13:11