使用class以特定格式编写xml

时间:2015-09-22 13:25:51

标签: c# xml

我试着通过将变量传递给类来了解如何编写xml。这是我的班级:

NSUserDefaults

我不明白如何实现这种格式:

using System.Xml.Serialization;

public partial class Output {

    private OutputReportType[] itemsField;


    [System.Xml.Serialization.XmlElementAttribute("ReportType", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public OutputReportType[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}


public partial class OutputReportType {

    private OutputReportTypeReportsReport[][] reportsField;

    private string nameField;


    public OutputReportTypeReportsReport[][] Reports {
        get {
            return this.reportsField;
        }
        set {
            this.reportsField = value;
        }
    }

        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}


public partial class OutputReportTypeReportsReport {

    private OutputReportTypeReportsReportItemsItem[][] itemsField;

    private string nameField;


    public OutputReportTypeReportsReportItemsItem[][] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }


    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Output output = new Output()
            {
                outputReportType = new List<OutputReportType>() {
                     new OutputReportType() {
                          outputReportTypeReportsReportItemsItem = new List<OutputReportTypeReportsReportItemsItem>() {
                              new OutputReportTypeReportsReportItemsItem() {
                                   name = "Abc"
                              }
                          }
                     }
                 }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(Output));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, output);
            writer.Flush();
            writer.Close();
            writer.Dispose();

        }
    }

    [XmlRoot("output")]
    public class Output
    {
        [XmlElement("OutputReportType")]
        public List<OutputReportType> outputReportType { get; set; }
    }

    [XmlRoot("OutputReportType")]
    public class OutputReportType 
    {
        [XmlElement("outputReportTypeReportsReportItemsItem")]
        public List<OutputReportTypeReportsReportItemsItem> outputReportTypeReportsReportItemsItem { get; set; }
    }

    [XmlRoot("OutputReportTypeReportsReportItemsItem")]
    public class OutputReportTypeReportsReportItemsItem 
    {
        [XmlElement("name")]
        public string name { get; set; }
    }

}




​