XDocument xdoc = new XDocument();
xdoc.Add(new XElement("Setting"));
XElement xel1 = new XElement("Port", "com5");
XElement xel2 = new XElement("baud", "115200");
xdoc.Root.Add(xel1);
xdoc.Root.Add(xel2);
XElement xtmp1 = new XElement(Name = "Socket", new XAttribute("IP", "192.168.1.15"), new XAttribute("Port", "8000"));
XElement xtmp2 = new XElement(Name = "Serialport", new XAttribute("Portname", "COM4"), new XAttribute("Baud", "115200"), new XAttribute("Databit", "8"));
XElement xel3 = new XElement(Name = "Factory", new XAttribute("name", "bxyz"), new XAttribute("init", "on"), xtmp1, xtmp2);
xdoc.Root.Add(xel3);
xdoc.Save(@"d:\a.xml");
XDocument xdoc2 = XDocument.Load(@"d:\a.xml");
var tmpcontents = xdoc2.Root.Elements().ToList();
foreach (var item in tmpcontents)
{
Console.WriteLine(item.Name.ToString());
}