如何做多个不同的类来生成 XML?
Posted
技术标签:
【中文标题】如何做多个不同的类来生成 XML?【英文标题】:How to do multiple different classes to generate XML? 【发布时间】:2021-07-02 01:19:34 【问题描述】:我正在尝试进行 XML 序列化。其中有多个不同的类,即 Header & Item。我创建了类 TransferOrder 来组合 Header 和 Item。 Header 做得很好,但 Item 显示了两次。
下面是我的 Program.cs
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
namespace XML_Serialization
class Program
public static void Main(string[] args)
Program p = new Program();
p.Final("item.xml");
public void Final(string filename)
XmlSerializer s = new XmlSerializer(typeof(TransferOrder));
TextWriter writer = new StreamWriter(filename);
TransferOrder c = new TransferOrder();
Header head = new Header();
head.DocNo = 0000000044;
head.MoveType = 311;
head.SourceStrType = 010;
head.SourceStrBin = "IQA";
head.DestStrType = 921;
head.DestStrBin = "TRANSFER";
head.Werks = 3006;
c.Header = head;
//Item Serialization
Item item1 = new Item();
item1.MaterialNo = 000000000010001251;
item1.Sloc = "KM22";
item1.Quantity = 5.000;
item1.UOM = "M2";
item1.PlantID = 3006;
item1.LineItem = 1;
Item item2 = new Item();
item2.MaterialNo = 000000000010001251;
item2.Sloc = "KM22";
item2.Quantity = 5.000;
item2.UOM = "M2";
item2.PlantID = 3006;
item2.LineItem = 2;
Item item3 = new Item();
item3.MaterialNo = 000000000010001251;
item3.Sloc = "KM22";
item3.Quantity = 5.000;
item3.UOM = "M2";
item3.PlantID = 3006;
item3.LineItem = 3;
Item[] ig = item1, item2, item3 ;
c.Item = ig;
s.Serialize(writer, c);
writer.Close();
public class TransferOrder
public Header Header get; set;
public Item []Item get; set;
public class Header
public int DocNo get; set;
public int MoveType get; set;
public int SourceStrType get; set;
public string SourceStrBin get; set;
public int DestStrType get; set;
public string DestStrBin get; set;
public int Werks get; set;
public class Item
public int MaterialNo get; set;
public string Sloc get; set;
public double Quantity get; set;
public string UOM get; set;
public int PlantID get; set;
public int LineItem get; set;
我想要的结果是这样的
<?xml version="1.0" encoding="utf-8"?><dtl:TransferOrder xmlns:dtl="http://FS.BizTalk.App.RFID.CommonTODetail.Schemas.DetailTO/2021/03">
<Header>
<DocNo>0000000044</DocNo>
<MoveType>311</MoveType>
<SourceStrType>010</SourceStrType>
<SourceStrBin>IQA</SourceStrBin>
<DestStrType>921</DestStrType>
<DestStrBin>TRANSFER</DestStrBin>
<Werks>3006</Werks>
</Header>
<Item>
<MaterialNo>000000000010001251</MaterialNo>
<SLoc>KM22</SLoc>
<Quantity>5.000</Quantity>
<UOM>M2</UOM>
<PlantId>3006</PlantId>
<LineItem>1</LineItem>
</Item>
<Item>
<MaterialNo>000000000010001251</MaterialNo>
<SLoc>KM22</SLoc>
<Quantity>5.000</Quantity>
<UOM>M2</UOM>
<PlantId>3006</PlantId>
<LineItem>2</LineItem>
</Item>
<Item>
<MaterialNo>000000000010001251</MaterialNo>
<SLoc>KM22</SLoc>
<Quantity>5.000</Quantity>
<UOM>M2</UOM>
<PlantId>3006</PlantId>
<LineItem>3</LineItem>
</Item>
</dtl:TransferOrder>
但是,如果你运行我的代码,我的代码输出有 2 Item Element 。这和我想要的不一样。
谁能帮我解决这个问题?
【问题讨论】:
【参考方案1】:添加[XmlElement]
:
public class TransferOrder
public Header Header get; set;
[XmlElement]
public Item []Item get; set;
(默认行为是在列表/数组上添加一个包装层;[XmlElement]
告诉它忽略它并直接转到子数据)
【讨论】:
以上是关于如何做多个不同的类来生成 XML?的主要内容,如果未能解决你的问题,请参考以下文章