如何使用XmlSerialization将名称空间添加到XML的根元素中

问题描述 投票:1回答:1

我需要从一个填充的类生成XML。

我做了:

[XmlRoot(Namespace = ""), Serializable()]    
public class FormBuilderDataset
{
    public FormBuilderDataset()
    {
        this.Form_template = new Form_template();
        this.Form_template_header = new Form_template_header();
        this.Form_template_footer = new Form_template_footer();
        this.Sections = new List<Sections> { };
        this.Section_form = new List<Section_form> { };
        this.Categories = new List<Categories> { };
        this.Element_category = new List<Element_category> { };
        this.Elements = new List<Elements> { };
        this.Answers = new List<Answers> { };
        this.Calculation_methods_scores = new List<Calculation_methods_scores> { };
        this.Defaults = new List<Defaults> { };
        this.Rules = new List<Rules> { };
        this.Rules_actions = new List<Rules_actions> { };
        this.Category_section = new List<Category_section> { };
    }

    [XmlElement(ElementName = "Form_template")]
    public Form_template Form_template { get; set; }
    [XmlElement(ElementName = "Form_template_header")]
    public Form_template_header Form_template_header { get; set; }
    [XmlElement(ElementName = "Form_template_footer")]
    public Form_template_footer Form_template_footer { get; set; }
    [XmlElement(ElementName = "Sections")]
    public List<Sections> Sections { get; set; }
    [XmlElement(ElementName = "Section_form")]
    public List<Section_form> Section_form { get; set; }
    [XmlElement(ElementName = "Categories")]
    public List<Categories> Categories { get; set; }
    [XmlElement(ElementName = "Element_category")]
    public List<Element_category> Element_category { get; set; }
    [XmlElement(ElementName = "Elements")]
    public List<Elements> Elements { get; set; }
    [XmlElement(ElementName = "Answers")]
    public List<Answers> Answers { get; set; }
    [XmlElement(ElementName = "Calculation_methods_scores")]
    public List<Calculation_methods_scores> Calculation_methods_scores { get; set; }
    [XmlElement(ElementName = "Defaults")]
    public List<Defaults> Defaults { get; set; }
    [XmlElement(ElementName = "Rules")]
    public List<Rules> Rules { get; set; }
    [XmlElement(ElementName = "Rules_actions")]
    public List<Rules_actions> Rules_actions { get; set; }
    [XmlElement(ElementName = "Category_section")]
    public List<Category_section> Category_section { get; set; }
}

并且在上完课后,我尝试了:

XmlSerializer serializer = new XmlSerializer(typeof(FormBuilderDataset));
            XmlWriterSettings settings = new XmlWriterSettings()
            {
                Indent = true,
                OmitXmlDeclaration = false
            };
            TextWriter stream = new StreamWriter(fileName);
            XmlWriter writer = XmlWriter.Create(stream, settings);

            serializer.Serialize(writer, form, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
            writer.Close();
            stream.Close();

但是我正在接收:

<?xml version="1.0" encoding="utf-8"?>
<FormBuilderDataset>
<Form_template>
<form_template_guid>eed0cb1e-2e60-44b4-a544-b2cbfa15001c</form_template_guid>
<form_template_name>SEM PARAR ||  SINERGIA V2</form_template_name>
<form_type_id>1</form_type_id>
<calculated_score_guid>1c8ce250-7bc6-43a5-bf77-bfff72c0f8f5</calculated_score_guid>
<displayed_max_score>0</displayed_max_score>
<is_max_scored_displayed>false</is_max_scored_displayed>
<has_auto_kpi>false</has_auto_kpi>
<kpi_version>0</kpi_version>
<kpi_creation_status>0</kpi_creation_status>
<description>SEM PARAR ||  SINERGIA V2</description>

...

我想返回:

<?xml version="1.0" standalone="yes"?>
<FormBuilderDataset xmlns="http://tempuri.org/FormBuilderDataset.xsd">
<Form_template>
<form_template_guid>eed0cb1e-2e60-44b4-a544-b2cbfa15001c</form_template_guid>
<form_template_name>SEM PARAR ||  SINERGIA V2</form_template_name>
<form_type_id>1</form_type_id>
<calculated_score_guid>1c8ce250-7bc6-43a5-bf77-bfff72c0f8f5</calculated_score_guid>
<displayed_max_score>0</displayed_max_score>
<is_max_scored_displayed>false</is_max_scored_displayed>
<has_auto_kpi>false</has_auto_kpi>
<kpi_version>0</kpi_version>
<kpi_creation_status>0</kpi_creation_status>
<description>SEM PARAR ||  SINERGIA V2</description>

..

因此,我想将Standalone =“ yes”属性添加到xml节点,而不是encoding =“ utf-8”,并将xmlns =“ http://tempuri.org/FormBuilderDataset.xsd”添加到FormBuilderDataset节点。

有人可以帮我吗?

c# .net xml serialization
1个回答
0
投票

他们是at子。

所以您需要添加类似内容

public class FormBuilderDataset
{
    [XmlAttribute] 
    public string xmlns{ get {return "http://tempuri.org/FormBuilderDataset.xsd";} }

...
}

还有另一个,这个..

[XmlAttribute] 
public string standalone { get {return "yes";} }

...可能不起作用,让我想想...

© www.soinside.com 2019 - 2024. All rights reserved.