techmakin techmakin - 3 months ago 15
C# Question

How to add an end tag on a List when using XML Serialization in c#?

I am trying to add an end tag when I try to serialize an xml file, and it's not working just right. Here is the class I am using

private List<RTACModuleDeviceConnectionSettingPagesSettingPageRowSetting> rowField;

/// <remarks/>
//[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItem("Setting", typeof(RTACModuleDeviceConnectionSettingPagesSettingPageRowSetting), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public List<RTACModuleDeviceConnectionSettingPagesSettingPageRowSetting> Row
{
get
{
return this.rowField;
}
set
{
this.rowField = value;
}
}


Here is the code to write the XML:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = new UTF8Encoding(false);
settings.Indent = true;
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
//settings.CloseOutput = true;
//settings.NewLineOnAttributes = true;
XmlSerializer xs = new XmlSerializer(deserializedDevice.GetType());

MemoryStream ms = new MemoryStream();

XmlWriter writer = XmlWriter.Create(ms, settings);
serializer.Serialize(writer, deserializedDevice, ns);

string text = Encoding.UTF8.GetString(ms.ToArray());
File.WriteAllText(path, Encoding.UTF8.GetString(ms.ToArray()));


I am expecting to get this written out:

<Row>
<Setting enabled="false">
<Column>Setting</Column>
<Value>Network Connection Type</Value>
</Setting>
<Setting>
<Column>Value</Column>
<Value>Telnet</Value>
</Setting>
<Setting>
<Column>Comment</Column>
<Value />
</Setting>
</Row>
<Row>
<Setting enabled="false">
<Column>Setting</Column>
<Value>Local Port Number</Value>
</Setting>
<Setting>
<Column>Value</Column>
<Value>5206</Value>
</Setting>
<Setting>
<Column>Comment</Column>
<Value />
</Setting>
</Row>


Instead I am missing my multiple end tags on Row:

<Row>
<Setting
enabled="false">
<Column>Setting</Column>
<Value>Network Connection Type</Value>
</Setting>
<Setting>
<Column>Value</Column>
<Value>Telnet</Value>
</Setting>
<Setting>
<Column>Comment</Column>
<Value />
</Setting>
<Setting
enabled="false">
<Column>Setting</Column>
<Value>Local Port Number</Value>
</Setting>
<Setting>
<Column>Value</Column>
<Value>5315</Value>
</Setting>
<Setting>
<Column>Comment</Column>
<Value />
</Setting>
</Row>

Answer

I ended up having to change the class to 2 different XMLElementAttributes to get it to work, Richard Matheson was very close. Setting and Row had to be separated.

[System.Xml.Serialization.XmlElementAttribute("Row", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public RTACModuleDeviceConnectionSettingPagesSettingPage[] Row
    {
        get
        {
            return this.rowField;
        }
        set
        {
            this.rowField = value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute("Setting", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public List<RTACModuleDeviceConnectionSettingPagesSettingPageRowSetting> Setting
    {
        get
        {
            return this.settingField;
        }
        set
        {
            this.settingField = value;
        }
    }
Comments