2

I need to generate a XML from a filled class.

I did:

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

and after fill the class, I've tried:

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

But I'm receiving:

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

...

I would like to return:

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

..

So, I would like to add standalone="yes" property to xml node, instead of encoding="utf-8", and add xmlns="http://tempuri.org/FormBuilderDataset.xsd" to FormBuilderDataset node.

Could someone help me?

mason
  • 31,774
  • 10
  • 77
  • 121
Bisneto
  • 141
  • 2
  • 13

1 Answers1

0

They are attributes.

So you need to add something like

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

...
}

I've not heard of the standalone thing is that something todo with Oracle?

Paul Sumpner
  • 447
  • 7
  • 8
  • Hi Paul, didn't work. I've tried public class FormBuilderDataset { [XmlAttribute] public string xmlns { get { return "http://tempuri.org/FormBuilderDataset.xsd"; } } But is generating: – Bisneto Oct 08 '19 at 18:42
  • Don't give up, it's something to do with attributes. What i suggested was a guess. Try changing it so it's a `{ get; set; }` or something. – Paul Sumpner Oct 08 '19 at 18:46
  • I've got this answer about standalone in Stackoverflow https://stackoverflow.com/questions/5578645/what-does-the-standalone-directive-mean-in-xml – Bisneto Oct 08 '19 at 18:47
  • No, never gave up, but actually I'm spending lots of time in this, without result. I've tried the changes, but is still didn't working. – Bisneto Oct 08 '19 at 18:58
  • Have you managed to get any attributes out? Look up how to serialize attributes. – Paul Sumpner Oct 08 '19 at 19:02
  • Ok, I will try it. To solve my problem, I did: string fileContent = System.IO.File.ReadAllText(fileName); fileContent = fileContent .Replace("", "") .Replace("", ""); File.WriteAllText(fileName, fileContent); – Bisneto Oct 09 '19 at 20:45