-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.

3
  • 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. Commented Apr 13, 2016 at 0:30
  • You could use something like this "stackoverflow.com/questions/7771500/…" Commented Apr 13, 2016 at 0:49
  • Thanks. But i would like the "Name" become the attribute and "Mike" should be the value for it. Commented Apr 13, 2016 at 1:02

1 Answer 1

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++;
            }
Sign up to request clarification or add additional context in comments.

4 Comments

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.
please see my answer update for that, the xml file will be created automatically if not present
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."
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.