在c#中使用XML序列化时如何在List上添加结束标记?

时间:2016-08-18 01:51:40

标签: c# xml serialization

我在尝试序列化xml文件时尝试添加结束标记,但它不能正常工作。这是我正在使用的课程

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

以下是编写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()));

我希望写出来:

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

相反,我在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>

1 个答案:

答案 0 :(得分:0)

我最终不得不将类更改为2个不同的XMLElementAttributes以使其工作,Richard Matheson非常接近。设置和行必须分开。

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