C# 使用XmlSerializer设置xml头

C# 使用XmlSerializer设置xml头,c#,xml-serialization,C#,Xml Serialization,我的xml看起来像 我有一个固定的头,我需要附加到我正在编写的任何xml文件中,我该怎么做 **<?xml version="1.0"?> <!DOCTYPE profile SYSTEM "criteria_profile.dtd">** <profile type="INTERC" id="6 " description="MushonTest" state="" lastchuser="MUSHON" lastchtmstmp="20130903132018

我的xml看起来像

我有一个固定的头,我需要附加到我正在编写的任何xml文件中,我该怎么做

**<?xml version="1.0"?>
<!DOCTYPE profile SYSTEM "criteria_profile.dtd">**
<profile type="INTERC" id="6 " description="MushonTest" state="" lastchuser="MUSHON" lastchtmstmp="20130903132018 " createuser="MUSHON">
    <root>
        <node type="A">
            <item description="crite3">
                <field>AUTHCKMAN<criterion sign="I" opt="EQ" low="cl" high=""/>
                </field>
                <field>JOBCLASS<criterion sign="I" opt="EQ" low="clas" high=""/>
                </field>
                <field>JOBNAME<criterion sign="I" opt="EQ" low="nam" high=""/>
                </field>
                <field>SDLUNAME<criterion sign="I" opt="EQ" low="creat" high=""/>
                </field>
            </item>
您只能通过XmlWriter实现这一点,而不能直接作为XmlSerializer API的一部分;但您仍然可以在此处使用XmlSerializer,例如:

var ser = new XmlSerializer(typeof(Foo));
using (var xw = XmlWriter.Create({any output device here}))
{
    xw.WriteDocType("profile", null, "criteria_profile.dtd", null);
    ser.Serialize(xw, new Foo());
}
由此产生:

<?xml version="1.0"?><!DOCTYPE profile SYSTEM "criteria_profile.dtd"><Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
您只能通过XmlWriter实现这一点,而不能直接作为XmlSerializer API的一部分;但您仍然可以在此处使用XmlSerializer,例如:

var ser = new XmlSerializer(typeof(Foo));
using (var xw = XmlWriter.Create({any output device here}))
{
    xw.WriteDocType("profile", null, "criteria_profile.dtd", null);
    ser.Serialize(xw, new Foo());
}
由此产生:

<?xml version="1.0"?><!DOCTYPE profile SYSTEM "criteria_profile.dtd"><Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

出于某种原因,这会从我的XML中删除出于某种原因,这会从我的XML中删除