Let's say we want to create xml like this:

<?xml version="1.0" encoding="utf-8"?>
<project>
  <menu bg_img="">
    <mnu_item txt="Menu item 1" anotherAttribute="another attribute 1">
      <sub_item txt="Sub item 1" articleId="1" />
    </mnu_item>
	
    <mnu_item txt="Menu item 2" anotherAttribute="another attribute 2">
      <sub_item txt="Sub item 2" articleId="2" />
    </mnu_item>
	
  </menu>
</project>

Serialization

First we need model:

  [XmlRoot("project")]
  public class Project
  {
    [XmlElement(ElementName = "menu")]
    public Menu Menu { get; set; }
  }

  public class Menu
  {
    [XmlAttribute("back_image")]
    public string backImage {get; set;}

    [XmlElement(ElementName = "mnu_item")]
    public List<MenuItem> MenuItem { get; set; }
  }

  public class MenuItem
  {
    [XmlAttribute("txt")]
    public string Txt { get; set; }

    [XmlAttribute("anotherAttribute")]
    public string AnotherAttribute { get; set; }

    [XmlElement("sub_item")]
    public SubItem SubItem { get; set; }
  }

  public class SubItem
  {
    [XmlAttribute("txt")]
    public string Txt { get; set; }

    [XmlAttribute("articleId")]
    public string ArticleId { get; set; }
  }

ViewModel looks like this:

Project serializedProject = new Project();

serializedProject.Menu = new Menu();
serializedProject.Menu.backImage = string.Empty;

MenuItem menuItem = new MenuItem();

menuItem.Txt = "Menu item 1";
menuItem.AnotherAttribute = "another attribute 1";

menuItem.SubItem = new SubItem();
menuItem.SubItem.Txt = "Sub item 1";
menuItem.SubItem.ArticleId = "1";

serializedProject.Menu.MenuItem = new List<MenuItem>();

serializedProject.Menu.MenuItem.Add(menuItem);

menuItem = new MenuItem();

menuItem.Txt = "Menu item 2";
menuItem.AnotherAttribute = "another attribute 2";

menuItem.SubItem = new SubItem();

menuItem.SubItem.Txt = "Sub item 2";
menuItem.SubItem.ArticleId = "2";

serializedProject.Menu.MenuItem.Add(menuItem);

TextWriter txtWriter = new StreamWriter(Path.ChangeExtension(System.Reflection.Assembly.GetEntryAssembly().Location, ".xml"));

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Project), "");
xmlSerializer.Serialize(txtWriter, serializedProject, ns);
txtWriter.Close();

Notice lines:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
...
xmlSerializer.Serialize(txtWriter, serializedProject, ns);

Without these lines our XML would look like this:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Then in model, line, for example:

[XmlAttribute("back_image")]

Means that our attribute will be named as "back_image" in our XML.

Also, notice line:

Path.ChangeExtension(System.Reflection.Assembly.GetEntryAssembly().Location, ".xml")

With

System.Reflection.Assembly.GetEntryAssembly().Location

We are geting location of our exe file, and with method Path.ChangeExtension we are changing our file extension to XML.

Linq to XML

For linq to XML we don't need model, only XElement, so my code looks like this:

XElement myLinq2XML = 
	new XElement(
		"project",
		new XElement(
			"menu",
			new XAttribute("back_image", string.Empty),
			new XElement(
				"mnu_item",
				new XAttribute("txt", "Linq2XML item 1"),
				new XAttribute("anotherAttribute", "another attribute 1"),
				new XElement("sub_item", 
					new XAttribute("txt", "Sub item 1"), 
					new XAttribute("articleId", "1")
				)
			),
			new XElement(
				"mnu_item",
				new XAttribute("txt", "Linq2XML item 2"),
				new XAttribute("anotherAttribute", "another attribute 2"),
				new XElement("sub_item", 
					new XAttribute("txt", "Sub item 2"), 
					new XAttribute("articleId", "2")
				)
			)
		)
	);

Example project you can download from here.