如何更改生成的XML的编码样式和名称空间?

时间:2015-08-13 07:01:25

标签: c# xml serialization

我需要以这种形式创建XML:



<?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>
&#13;
&#13;
&#13;

我正在使用此代码,其中对象ccTop包含所有必需的类:

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


     }

但是输出XMl具有编码风格&#34; utf-8&#34; (小写)和不同的命名空间如下:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

关于如何使它们完全相似的任何想法?

3 个答案:

答案 0 :(得分:0)

TextWriter writer = new StreamWriter(outpath , true, Encoding.ASCII);

您可以尝试使用这些代码

答案 1 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            saveXmlPath();
        }
        const string FILENAME = @"c:\temp\test.xml";
        public static void saveXmlPath()
        {
            CastleConfigTop ccTop = new CastleConfigTop();

            XmlSerializer serializer = new XmlSerializer(typeof(CastleConfigTop));
           XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("xsi","xsd/c5c.xsd");
            ns.Add("", "http://www.w3.org/2001/XMLSchema-instance");

            using (TextWriter writer = new StreamWriter(FILENAME))
            {
                serializer.Serialize(writer, ccTop, ns);
            }


        }
    }
    [XmlRoot(ElementName = "CastleConfigTop", Namespace = "xsi")]
    public class CastleConfigTop
    {
    }
}
​

答案 2 :(得分:0)

您可以使用LINQ to XML对此进行后处理,以获得您想要的结果。

创建XDocument并将对象序列化为:

var doc = new XDocument();

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

然后修改声明和属性:

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));

然后写入光盘。请注意,您可以使用XmlWriterSettings设置各种写入选项,例如缩进。

var settings = new XmlWriterSettings
{
    Indent = true
};

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

或者,如果您可以使用Save(这会给您更少的选项,但默认值应该没问题):

doc.Save(FILENAME);

我不希望声明是小写的问题,但是如果它是你必须使用其他方法修改它 - XmlWriter需要Encoding并将转换此内部为小写形式,因此在XDocument中更改此内容无效。