将 C# 模型序列化为 xml 数据

Posted

技术标签:

【中文标题】将 C# 模型序列化为 xml 数据【英文标题】:Serializing C# model into a xml data 【发布时间】:2021-04-25 02:25:27 【问题描述】:

我正在尝试使用 C# 模型生成具有以下结构的 xml 文件:

<root>
  <row>
    <field name="placeholder">Test field 1</field>
    <field name="placeholder">Test field 2</field>
    <field name="placeholder">Test field 3</field>
    <field name="placeholder">Test field 4</field>
    <field name="placeholder">Test field 5</field>
  </row>
  <row>
    <field name="placeholder">Test field 1</field>
    <field name="placeholder">Test field 2</field>
    <field name="placeholder">Test field 3</field>
    <field name="placeholder">Test field 4</field>
    <field name="placeholder">Test field 5</field>
  </row>
</root>

我已经设置了这些模型,但我得到的输出完全错误

public class root

    public List<row> rows get; set;


public class row

    public List<field> fields get; set;


public class field

    [XmlAttribute("name")]
    public string AttributeName  get; set; 

    [XmlText]
    public string AttributeValue  get; set; 


这是我用来创建模型的代码

 var root = new root();
 root.rows = new List<row>();

 for (int x = 0; x < 2; x++)
 
      root.rows.Add(new row()
      
           fields = new List<field>()
           
                new field()
                
                      AttributeName="placeholder",
                      AttributeValue="test1",
                ,
                new field()
                
                      AttributeName="placeholder",
                      AttributeValue="test2",
                ,
                new field()
                
                      AttributeName="placeholder",
                      AttributeValue="test3",
                ,
                new field()
                
                      AttributeName="placeholder",
                      AttributeValue="test4",
                ,
                new field()
                
                      AttributeName="placeholder",
                      AttributeValue="test5",
                
           
    );

所以根应该有多个可以在代码中定义的行,但是,这些行将有多个字段,只是具有不同的属性名称和值。

新行将始终循环添加,因为循环中有数据需要填写字段属性和值。

我面临的问题是当我运行它时,我得到以下输出:

<root>
    <rows>
        <row>
            <fields>
                <field name="placeholder">test1</field>
                <field name="placeholder">test2</field>
                <field name="placeholder">test3</field>
                <field name="placeholder">test4</field>
                <field name="placeholder">test5</field>
            </fields>
        </row>
        <row>
            <fields>
                <field name="placeholder">test1</field>
                <field name="placeholder">test2</field>
                <field name="placeholder">test3</field>
                <field name="placeholder">test4</field>
                <field name="placeholder">test5</field>
            </fields>
        </row>
    </rows>
</root>

【问题讨论】:

问题出在哪里? 抱歉!将问题添加到问题中 【参考方案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)
         
             root root = new root()
             
                 rows = new List<row>() 
                    new row() 
                        fields = new List<field>() 
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 1",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 2",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 3",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 4",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 5"
                        
                    ,
                    new row() 
                        fields = new List<field>() 
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 1",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 2",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 3",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 4",
                            new field()  AttributeName = "placeholder", AttributeValue = "Test Field 5"
                        
                    
                
             ;

             XmlWriterSettings settting = new XmlWriterSettings();
             settting.Indent = true;
             XmlWriter writer = XmlWriter.Create(FILENAME, settting);

             XmlSerializer serializer = new XmlSerializer(typeof(root));
             serializer.Serialize(writer, root);
 
  
         
    
    public class root
    
        [XmlElement(ElementName = "row")]
        public List<row> rows  get; set; 
    

    public class row
    
        [XmlElement("field")]
        public List<field> fields  get; set; 
    

    public class field
    
        [XmlAttribute("name")]
        public string AttributeName  get; set; 

        [XmlText]
        public string AttributeValue  get; set; 

    
 

【讨论】:

以上是关于将 C# 模型序列化为 xml 数据的主要内容,如果未能解决你的问题,请参考以下文章

将 ODATA xml 序列化/反序列化为 C# 对象

如何将 XML 反序列化为 C# 中的对象? [复制]

需要帮助将 XML 文件反序列化为对象 C#

将 Web 服务 API 中的 XML 字符串反序列化为 C# 对象

将 XML 文件反序列化为 C# 类对象时出错

C#将不同类型的xml数组反序列化为多个数组