如何在ASP.NET Core中用C#生成带METADATA的XML文件?
如何在ASP.NET Core中自动生成带METADATA标签的XML格式?
问题背景
我需要从数据库获取数据并转换为对象列表,最终生成带有METADATA标签的XML文件(期望格式如下)。希望DATAPACKET、METADATA这类外层标签无需手动编写,想知道ASP.NET Core中是否有C#类库可在序列化数据时自动生成它们。
尝试的代码
public class Book { public string title; public string author; public string publisher; public double price; } public void GenerateXML() { var overview = new List<Book>{ new Book() { title = "This is a book", author = "Somebody wrote it", publisher = "Someone published it", price = 999.99 }, new Book() { title = "This is a book2", author = "Somebody wrote it2", publisher = "Someone published it2", price = 101010 } }; System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(Book)); var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//myXmFile.xml"; System.IO.FileStream file = System.IO.File.Create(path); foreach (var item in overview) { writer.Serialize(file, item); } file.Close(); }
当前输出XML
<?xml version="1.0" encoding="utf-8"?> <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <title> This is a book </title> <author> Somebody wrote it </author> <publisher> Someone published it </publisher> <price> 999.99 </price> </Book> <?xml version="1.0" encoding="utf-8"?> <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <title> This is a book2 </title> <author> Somebody wrote it2 </author> <publisher> Someone published it2 </publisher> <price> 101010 </price> </Book>
期望的XML格式
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DATAPACKET Version="2.0"> <METADATA> <FIELDS> <FIELD attrname="title" fieldtype="string"/> <FIELD attrname="author" fieldtype="string"/> <FIELD attrname="publisher" fieldtype="string"/> <FIELD attrname="price" fieldtype="r8"/> </FIELDS> <PARAMS/> </METADATA> <ROWDATA> <ROW title="This is a book" author="Somebody wrote it" publisher="Someone published it" price="999.99" /> <ROW title="This is a book2" author="Somebody wrote it2" publisher="Someone published it2" price="101010" /> </ROWDATA> </DATAPACKET>
解决方案
.NET原生没有专门针对该格式的序列化类库,但你要的格式正是DataSet默认的XML序列化结构,可以利用DataSet和DataTable自动生成包含METADATA的XML,无需手动编写外层标签。
实现步骤
- 将对象列表转换为
DataTable - 将
DataTable添加到DataSet - 使用
DataSet.WriteXml方法生成XML(指定XmlWriteMode.WriteSchema模式,自动包含元数据)
修改后的代码
public class Book { public string title; public string author; public string publisher; public double price; } // 将Book列表转换为DataTable private DataTable ConvertBooksToDataTable(List<Book> books) { DataTable table = new DataTable(); // 定义列和对应类型 table.Columns.Add("title", typeof(string)); table.Columns.Add("author", typeof(string)); table.Columns.Add("publisher", typeof(string)); table.Columns.Add("price", typeof(double)); // 填充数据 foreach (var book in books) { table.Rows.Add(book.title, book.author, book.publisher, book.price); } return table; } public void GenerateXML() { var overview = new List<Book> { new Book() { title = "This is a book", author = "Somebody wrote it", publisher = "Someone published it", price = 999.99 }, new Book() { title = "This is a book2", author = "Somebody wrote it2", publisher = "Someone published it2", price = 101010 } }; DataTable bookTable = ConvertBooksToDataTable(overview); DataSet dataSet = new DataSet(); dataSet.Tables.Add(bookTable); // 生成XML文件 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myXmFile.xml"); // WriteSchema模式会自动包含METADATA等结构 dataSet.WriteXml(path, XmlWriteMode.WriteSchema); }
替代方案:自定义序列化类
如果不想依赖DataSet,可以手动定义匹配目标XML结构的类,并通过System.Xml.Serialization特性标记,再序列化整个对象结构。这种方式需要手动编写所有层级的类,但灵活性更高。示例核心类结构如下:
[XmlRoot("DATAPACKET")] public class DataPacket { [XmlAttribute("Version")] public string Version { get; set; } = "2.0"; [XmlElement("METADATA")] public Metadata Metadata { get; set; } [XmlElement("ROWDATA")] public RowData RowData { get; set; } } public class Metadata { [XmlElement("FIELDS")] public Fields Fields { get; set; } [XmlElement("PARAMS")] public Params Params { get; set; } = new Params(); } // 其他类(FIELDS、FIELD、ROWDATA、ROW)同理,用Xml特性标记对应节点和属性
总结
推荐使用DataSet方案,无需手动编写DATAPACKET、METADATA等外层标签,自动生成符合要求的XML结构,实现成本最低。
内容的提问来源于stack exchange,提问作者Jessie Loke
相关产品推荐
相关产品推荐

