【问题标题】:How to add namespaces to root element of the XML with XmlSerialization如何使用 XmlSerialization 将命名空间添加到 XML 的根元素
【发布时间】:2019-10-08 18:25:40
【问题描述】:

我需要从填充类生成 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】:

    它们是属性。

    所以你需要添加类似的东西

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

    我没有听说过 standalone 的事情是与 Oracle 相关的吗?

    【讨论】:

    • 嗨,保罗,没用。我试过 public class FormBuilderDataset { [XmlAttribute] public string xmlns { get { return "tempuri.org/FormBuilderDataset.xsd";但正在生成:
    • 不要放弃,这和属性有关。我的建议是猜测。尝试将其更改为 { get; set; } 或其他名称。
    • 我在 Stackoverflow stackoverflow.com/questions/5578645/…得到了关于独立的答案@
    • 不,从未放弃,但实际上我在这方面花了很多时间,但没有结果。我已经尝试了更改,但仍然无法正常工作。
    • 你有没有设法得到任何属性?查看如何序列化属性。
    猜你喜欢
    • 2013-06-03
    • 2023-03-22
    • 2017-11-27
    • 2011-02-10
    • 1970-01-01
    • 2016-05-10
    • 2013-04-05
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多