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++;
}