c# - 多个子 c# 上的反序列化 xml
问题描述
我有一个发票列表,我必须反序列化它。首先我序列化为 xml,它很容易找到和应用。这次我不能反序列化 xml 中的项目列表。例如,这是我的 xml 序列化行:
<?xml version="1.0" encoding="utf-8"?>
<Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Invoice>4711</Invoice>
<Table>8</Table>
<Outlet>3</Outlet>
<User>17</User>
<Creation>20140101</Creation>
<Item>
<Type>Revenue</Type>
<Productgroup>5</Productgroup>
<TotalAmount>6</TotalAmount>
<TaxClassification>1</TaxClassification>
<Text>Pizza Tonna</Text>
</Item>
<Item>
<Type>Gratuity</Type>
<TotalAmount>1.5</TotalAmount>
<Text>Tip</Text>
</Item>
<Item>
<Type>Payment</Type>
<TotalAmount>7.5</TotalAmount>
<ResNo>4812</ResNo>
</Item>
</Body>
如您所见,我有一个 Body 根 xml 和 5 个主要行,但在深处我有多个 Item 数据,它是一个列表。如果我每张发票都有一个项目,这很容易做到,但在艰苦的研究中,我无法得到任何意义。
让我和你谈谈我做了什么。这些是我的身体和物品类:
[XmlRoot("Body")]
public class Body
{
[XmlElement("Invoice")]
public int Invoice { get; set; }
[XmlElement("Table")]
public int Table { get; set; }
[XmlElement("Outlet")]
public int Outlet { get; set; }
[XmlElement("User")]
public int User { get; set; }
[XmlElement("Creation")]
public int Creation { get; set; }
[XmlElement("Item")]
public List<Item> Items { get; set; }
}
[XmlRoot("Item")]
public class Item
{
[XmlElement("Type")]
public string? Type { get; set; }
[XmlElement("Productgroup")]
public int? Productgroup { get; set; }
public bool ShouldSerializeProductgroup()
{
return Productgroup.HasValue;
}
[XmlElement("TotalAmount")]
public double? TotalAmount { get; set; }
public bool ShouldSerializeTotalAmount()
{
return TotalAmount.HasValue;
}
[XmlElement("TaxClassification")]
public double? TaxClassification { get; set; }
public bool ShouldSerializeTaxClassification()
{
return TaxClassification.HasValue;
}
[XmlElement("Text")]
public string? Text { get; set; }
[XmlElement("ResNo")]
public int? ResNo { get; set; }
public bool ShouldSerializeResNo()
{
return ResNo.HasValue;
}
}
这是我的主要序列化程序,反之亦然。
static void Main(string[] args)
{
try
{
Body body = new Body
{
Invoice = 4711,
Table = 8 / 1,
Outlet = 3,
User = 17,
Creation = 20140101,
Items = new List<Item>()
{
new Item{Type="Revenue",Productgroup = 5,TotalAmount=6.00,TaxClassification=1,Text="Pizza Tonna"},
new Item{Type="Gratuity",TotalAmount=1.50,Text="Tip"},
new Item{Type="Payment",TotalAmount=7.50,ResNo=4812}
}
};
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Body));
StreamWriter sw = new StreamWriter("CloseInvoice.xml");
xmlSerializer.Serialize(sw, body);
sw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Body));
StreamReader sr = new StreamReader("CloseInvoice.xml");
Body body = (Body)xmlSerializer.Deserialize(sr);
Console.WriteLine("Invoice Information");
Console.WriteLine("Type: "+body.Invoice);
Console.WriteLine("Table: " + body.Table);
Console.WriteLine("Outlet: " + body.Outlet);
Console.WriteLine("User: " + body.User);
Console.WriteLine("Creation: " + body.Creation);
}
catch (Exception)
{
throw;
}
}
我尝试了所有循环,但无法在控制台上编写项目。你能帮我么?从现在开始感谢。
编辑:这是我的控制台。我可以获取正文行,但我也想在 xml 中获取项目列表。
解决方案
你有什么问题 ?它对我有用:
for (int i = 0; i < body.Items.Count; i++)
{
var item = body.Items[i];
Console.WriteLine("ProductGroup: " + item.Productgroup);
Console.WriteLine("ResNo: " + item.ResNo);
Console.WriteLine("TaxClassification: " + item.TaxClassification);
Console.WriteLine("Text: " + item.Text);
Console.WriteLine("TotalAmount: " + item.TotalAmount);
Console.WriteLine("Type: " + item.Type);
}
推荐阅读
- sql - 如何在迁移文件中添加新列
- django-rest-framework - 如何使用 drf_yasg 显示排序
- instagram - Instagram 图形 api 获得关注和追随者数量
- reactjs - 如何解决 testing-library-react 中的“更新未包含在 act() 中”警告?
- python - 在 tkinter 中重复创建和删除对象(例如矩形)时间延迟
- asp.net-core-mvc - MVC 核心验证 - 最初呈现为有效
- c# - 使用枚举的计算器
- javascript - Wordpress:是否可以在块编辑器样式表中使用 post-type 作为 CSS 选择器的一部分?
- c - 连接字符串时不显示几个字符
- python - 将文本格式的固定宽度表格转换为dataframe/excel/csv