Set xml header with XmlSerializer

瘦欲@ 提交于 2019-12-14 02:27:07

问题


My xml looks like that...

I have this fixed header I need to append to any xml file I am writing, an idea how do I do that?

**<?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>

回答1:


You can only do that via XmlWriter - not directly as part of the XmlSerializer API; but you can still use XmlSerializer here, for example:

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

which generates:

<?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" />


来源:https://stackoverflow.com/questions/18956954/set-xml-header-with-xmlserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!