Q:

How to change the generated XML's encoding style and name space?

I need to creat an XML in this form:

<?xml version="1.0" encoding="UTF-8"?>
<CastleConfigTop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xsd/c5c.xsd" Format="1">
  <ProjectInfo ProjectCode="1" ProjectIdentifier="C5_Valeo_SL">
    <ProjectName>TheProjectName</ProjectName>
  </ProjectInfo>
  <Options />
  <ConfigFile Name="C5_Valeo_SL">
    <History>
     
    </History>
    <Includes>
      
    </Includes>
    <Options>
      
    </Options>
    <DataTypes>
      <Options>
        <options />
      </Options>
      <ArgTypes />
      <Enums />
      <Lookups />
      <Doc />
    </DataTypes>
    <Interfaces />
    <Modules>
      
    </Modules>
  
    <ExexutionUnits />
    <Doc></Doc>
  </ConfigFile>
</CastleConfigTop>

And im using this code, where the object ccTop contains all the necessary classes:
 public void saveXmlPath()
     {
           //xmldoc.Save(this.OutputPath+"\\"+projectName+".C5C");
         XmlSerializer serializer = new XmlSerializer(typeof(CastleConfigTop));
         string outpath = this.OutputPath + "\\" + projectName + ".C5C";
         using (TextWriter writer = new StreamWriter(@outpath))
         {
             serializer.Serialize(writer, ccTop);
         }

     }

But the output XMl has the encoding style "utf-8" (lower case) and a different namespace like this:

<?xml version="1.0" encoding="utf-8"?>
<CastleConfigTop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ProjectInfo ProjectCode="1" ProjectIdentifier="C5_Valeo_SL">
    <ProjectName> ProjectName </ProjectName>
  </ProjectInfo>
  <Options />
  <ConfigFile Name="C5_Valeo_SL">
    <History>  
    </History>
    <Includes> 
    </Includes>
    <Options>  
    </Options>
    <DataTypes>
      <Options />
      <ArgTypes />
      <Enums />
      <Lookups />
      <Doc />
    </DataTypes>
    <Interfaces />
    <Modules> 
    </Modules>
    <Difs>  
    </Difs>
    <ExexutionUnits />
    <Doc>SAS Volvo SPA</Doc>
  </ConfigFile>
</CastleConfigTop>

Any Idea about how to make them exactly similar?

A:

You can post-process this using LINQ to XML to get what you want.
Create an XDocument and serialize your object to this:
var doc = new XDocument();

using (var writer = doc.CreateWriter())
{
    serializer.Serialize(writer, ccTop);
}

Then modify the declaration and attributes:
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

doc.Root.Attributes()
    .Where(a => a.IsNamespaceDeclaration && a.Value == "http://www.w3.org/2001/XMLSchema")
    .Remove();

doc.Root.Add(new XAttribute(xsi + "noNamespaceSchemaLocation", "xsd/c5c.xsd"));
doc.Root.Add(new XAttribute("Format", 1));

And then write to disc.  Note you can use XmlWriterSettings to set various options for writing, such as indentation.
var settings = new XmlWriterSettings
{
    Indent = true
};

using (var writer = XmlWriter.Create(FILENAME, settings))
{
    doc.WriteTo(writer);
}

Or if you can just use Save (which gives you less options, but the defaults should be fine):
doc.Save(FILENAME);

I wouldn't expect the declaration to be an issue in lower case, but if it is you'll have to modify this using some other means - XmlWriter takes an Encoding and will convert this internally to the lowercase form, so changing this in the XDocument won't help.

