You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

关于使用C#实现指定结构XML序列化的技术问询

没问题,我帮你搞定这个C# XML序列化的需求。咱们一步步来,先理清目标XML的完整结构,再写对应的实体类和序列化代码:

目标XML结构(补全截断部分):

<?xml version="1.0" dateprodstart="20180319" heureprodstart="12:08:36" dateprodend="20180319" heureprodend="12:12:45" version="1.21" encoding="utf-8"?>
<ListItems>
  <item>
    <filename>test5</filename>
    <destination>O</destination>
    <test1>EVA00</test1>
    <test2>ko</test2>
  </item>
  <item>
    <filename>test</filename>
    <destination>O</destination>
    <test1>xxxx</test1>
    <test2>ok</test2>
  </item>
</ListItems>
1. 定义对应数据结构的实体类

首先创建和XML节点映射的C#类,用System.Xml.Serialization命名空间的特性来绑定节点关系:

using System.Collections.Generic;
using System.Xml.Serialization;

// 根节点ListItems的映射类
[XmlRoot("ListItems")]
public class ListItems
{
    // 多个item子节点,用XmlElement标注每个节点名称
    [XmlElement("item")]
    public List<Item> Items { get; set; } = new List<Item>();
}

// item节点的映射类
public class Item
{
    [XmlElement("filename")]
    public string Filename { get; set; }

    [XmlElement("destination")]
    public string Destination { get; set; }

    [XmlElement("test1")]
    public string Test1 { get; set; }

    [XmlElement("test2")]
    public string Test2 { get; set; }
}
2. 处理XML声明的自定义属性

默认的XmlSerializer只会生成标准的XML声明(仅包含versionencoding),但你需要的声明里有额外的自定义属性(dateprodstartheureprodstart等),所以咱们得用XmlWriter手动写出这个声明,再序列化内容。

3. 完整的序列化代码示例

下面是包含测试数据、配置、序列化的完整代码,直接运行就能生成目标XML文件:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

class Program
{
    static void Main(string[] args)
    {
        // 1. 构造测试数据
        var listItems = new ListItems
        {
            Items = new List<Item>
            {
                new Item
                {
                    Filename = "test5",
                    Destination = "O",
                    Test1 = "EVA00",
                    Test2 = "ko"
                },
                new Item
                {
                    Filename = "test",
                    Destination = "O",
                    Test1 = "xxxx",
                    Test2 = "ok"
                }
            }
        };

        // 2. 配置XmlWriter参数:指定utf-8编码,关闭自动生成声明(手动写)
        var settings = new XmlWriterSettings
        {
            Encoding = System.Text.Encoding.UTF8,
            Indent = true, // 格式化输出,提升可读性
            OmitXmlDeclaration = true // 禁止自动生成标准声明
        };

        // 3. 写入XML文件
        using (var writer = XmlWriter.Create("output.xml", settings))
        {
            // 手动写出带自定义属性的XML声明
            writer.WriteProcessingInstruction("xml", 
                "version=\"1.0\" dateprodstart=\"20180319\" heureprodstart=\"12:08:36\" " +
                "dateprodend=\"20180319\" heureprodend=\"12:12:45\" version=\"1.21\" encoding=\"utf-8\"");

            // 序列化ListItems对象到XmlWriter
            var serializer = new XmlSerializer(typeof(ListItems));
            serializer.Serialize(writer, listItems);
        }

        Console.WriteLine("XML文件已成功生成!");
    }
}
小提示
  • 注意XML声明里重复出现了version属性(标准的version="1.0"和自定义的version="1.21"),虽然语法上允许,但容易造成混淆,建议把自定义版本属性改名为docVersion="1.21"之类的更清晰的名称。
  • 如果需要动态生成日期时间属性值,可以用DateTime.Now.ToString("yyyyMMdd")DateTime.Now.ToString("HH:mm:ss")来替换固定值。

内容的提问来源于stack exchange,提问作者user2076889

火山引擎 最新活动