Three XML formatting issues with .NET XmlSerializer

I have three problems with some data that I am serializing.

First of all, it gives < ?xml version="1.0" encoding="utf-8"?>, but the program I load it into only wants <?xml version="1.0"?>

Secondly, whenever the data is empty, it will use the shortcut to close the ( <z303-profile />) tag , but the program I am loading it into will not accept it and require<z303-profile></z303-profile>

Finally, I have some data that I cannot guarantee how long it will be, so I have it on the list. I need each element to have its own z305 header, but it displays the name of the list in which they are stored, at the beginning where everything happens. It is displayed as follows

    <z305List>
      <z305>
        ....
      </z305>
      <z305>
        ....
      </z305>
    </z305List>

the list is saved as

[XmlArrayItem("z305")]
public List<LocalPatronInfo> z305List = new List<LocalPatronInfo>();

The code I use for serialization is as follows

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer xmls = new XmlSerializer(typeof(AllRecords));
TextWriter tw = new StreamWriter(richTextBoxWorkingDir.Text + "\\" + filename);
xmls.Serialize(tw, allRecords, ns);
tw.Close();
+3
1

, . , , ( ?: -P):

XmlSerializer XmlWriter XML. , XMlWriter.. . :

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(TextWriter sink) : base(sink) { }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }

    public override void WriteStartDocument()
    {
        base.WriteRaw("<?xml version=\"1.0\"?>");
    }
}

public class temp
{
    public int a = 0;
    public List<int> x = new List<int>();
}

class Program
{
    static void Main(string[] args)
    {
        XmlTextWriterFull writer = new XmlTextWriterFull(Console.Out);

        XmlSerializer xs = new XmlSerializer(typeof(temp));
        xs.Serialize(writer,new temp());
        Console.ReadKey();
    }
}
+1

Source: https://habr.com/ru/post/1759982/


All Articles