帮我看看这个xml,怎么写个对应的实体类,用来作序列化的。

winner2050 2011-11-04 01:10:16
<Workbook>
<DocumentProperties>
<Created>2011-11-03T12:11:54Z</Created>
<Version>03.1015</Version>
</DocumentProperties>
<Worksheet Name="第1页">
<Table>

<Row Index="12">
<Cell Index="2">
<Data Type="String">26岁至35岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">9</Data>
</Cell>
<Cell Index="6">
<Data Type="String">研究生</Data>
</Cell>
<Cell Index="7">
<Data Type="Number">22</Data>
</Cell>
</Row>
<Row Index="13">
<Cell Index="2">
<Data Type="String">36岁至45岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">10</Data>
</Cell>
<Cell Index="6">
<Data Type="String">本科</Data>
</Cell>
<Cell Index="7">
<Data Type="Number">23</Data>
</Cell>
</Row>
<Row Index="14">
<Cell Index="2">
<Data Type="String">46岁至55岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">11</Data>
</Cell>
<Cell Index="6">
<Data Type="String">大专</Data>
</Cell>
<Cell Index="7">
<Data Type="Number">24</Data>
</Cell>
</Row>

</Table>
</Worksheet>
</Workbook>


...全文
229 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2011-11-04
  • 打赏
  • 举报
回复
有必要弄个实体类?
winner2050 2011-11-04
  • 打赏
  • 举报
回复
此实体类用于序列化反序列化,轻松实现了用友华表的录入。
huangwenquan123 2011-11-04
  • 打赏
  • 举报
回复

public class Workbook
{
private DocumentProperties doc;
[XmlElement(ElementName = "DocumentProperties")]
public DocumentProperties Doc
{
get { return doc; }
set { doc = value; }
}
private Worksheet[] worksheet;
[XmlElement(ElementName = "Worksheet")]
public Worksheet[] Worksheet
{
get { return worksheet; }
set { worksheet = value; }
}
}
public class DocumentProperties
{
private string created;
public string Created
{
get { return created; }
set { created = value; }
}
private string version;
public string Version
{
get { return version; }
set { version = value; }
}
}
public class Worksheet
{
private string name;
[XmlAttribute(AttributeName = "Name")]
public string Name
{
get { return name; }
set { name = value; }
}
private Table[] table;
[XmlElement(ElementName="Table")]
public Table[] Table
{
get { return table; }
set { table = value; }
}
}
public class Table
{
private Row[] row;
[XmlElement(ElementName = "Row")]
public Row[] Row
{
get { return row; }
set { row = value; }
}
}
public class Row
{
private int index;
[XmlAttribute(AttributeName = "Index")]
public int Index
{
get { return index; }
set { index = value; }
}
private Cell[] cell;
[XmlElement(ElementName = "Cell")]
public Cell[] Cell
{
get { return cell; }
set { cell = value; }
}
}
public class Cell
{
private int index;
[XmlAttribute(AttributeName = "Index")]
public int Index
{
get { return index; }
set { index = value; }
}
private Data data;
public Data Data
{
get { return data; }
set { data = value; }
}
}
public class Data
{
private string type;
[XmlAttribute(AttributeName = "Type")]
public string Type
{
get { return type; }
set { type = value; }
}
private string text;
[XmlText]
public string Text
{
get { return text; }
set { text = value; }
}
}

        static void Main(string[] args)
{

Workbook workbook = new Workbook();
DocumentProperties doc = new DocumentProperties();
doc.Created = "2011-11-03T12:11:54Z";
doc.Version = "03.1015";
workbook.Doc = doc;
Cell[] cell1 = new Cell[2] { new Cell() { Index = 2, Data = new Data() { Type = "String", Text = "26岁至35岁" } }, new Cell() { Index = 3, Data = new Data() { Type = "Number", Text = "9" } } };
Cell[] cell2 = new Cell[2] { new Cell() { Index = 2, Data = new Data() { Type = "String", Text = "36岁至45岁" } }, new Cell() { Index = 3, Data = new Data() { Type = "Number", Text = "10" } } };
Cell[] cell3 = new Cell[2] { new Cell() { Index = 2, Data = new Data() { Type = "String", Text = "46岁至55岁" } }, new Cell() { Index = 3, Data = new Data() { Type = "Number", Text = "11" } } };
Row[] row = new Row[3] { new Row() { Index = 12, Cell = cell1 }, new Row() { Index = 13, Cell = cell2 }, new Row() { Index = 14, Cell = cell3 } };
Table[] table = new Table[1] { new Table() { Row = row } };
Worksheet[] worksheet = new Worksheet[1] { new Worksheet() { Name = "第一页", Table = table } };
workbook.Worksheet = worksheet;
XmlSerializer xml = new XmlSerializer(workbook.GetType());
FileStream fs = new FileStream(@"E:\a.xml", FileMode.Create, FileAccess.Write);
xml.Serialize(fs, workbook);
fs.Close();
fs.Dispose();
Console.ReadLine();
}

<?xml version="1.0"?>
<Workbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DocumentProperties>
<Created>2011-11-03T12:11:54Z</Created>
<Version>03.1015</Version>
</DocumentProperties>
<Worksheet Name="第一页">
<Table>
<Row Index="12">
<Cell Index="2">
<Data Type="String">26岁至35岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">9</Data>
</Cell>
</Row>
<Row Index="13">
<Cell Index="2">
<Data Type="String">36岁至45岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">10</Data>
</Cell>
</Row>
<Row Index="14">
<Cell Index="2">
<Data Type="String">46岁至55岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">11</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
黄亮 2011-11-04
  • 打赏
  • 举报
回复
我用XmlSerializer做了序列化和反序,文件一致的,应该符合你的需要
黄亮 2011-11-04
  • 打赏
  • 举报
回复
[Serializable]
public class Workbook
{
public DocumentProperties DocumentProperties { get; set; }

public Worksheet Worksheet { get; set; }
}

[Serializable]
public class Worksheet
{
[XmlAttribute("Name")]
public string Name { get; set; }

public List<Row> Table { get; set; }
}

[Serializable]
public class Table
{
public List<Row> Row { get; set; }
}

[Serializable]
public class Row
{
[XmlAttribute("Index")]
public int Index { get; set; }

[XmlElement]
public List<Cell> Cell { get; set; }
}

[Serializable]
public class Cell
{
[XmlAttribute("Index")]
public int Index { get; set; }

public Data Data { get; set; }
}

[Serializable]
public class Data
{
[XmlAttribute("Type")]
public string Type { get; set; }

[XmlText]
public string Value { get; set; }
}

[Serializable]
public class DocumentProperties
{
public DateTime Created { get; set; }
public string Version { get; set; }
}
md5e 2011-11-04
  • 打赏
  • 举报
回复
实体只有这一部分
<Cell Index="2">
<Data Type="String">26岁至35岁</Data>
</Cell>
<Cell Index="3">
<Data Type="Number">9</Data>
</Cell>
<Cell Index="6">
<Data Type="String">研究生</Data>
</Cell>
<Cell Index="7">
<Data Type="Number">22</Data>
</Cell>
种草德鲁伊 2011-11-04
  • 打赏
  • 举报
回复
不一定要有实体类吧,自定一个datatable跟xml互转的方法不行么..
xiangaylian 2011-11-04
  • 打赏
  • 举报
回复
年龄段,数量,学历,数量
看样子似乎应该是个员工表的汇总信息啊。
就做一个包含这四个属性的实体不行么?
telankes2000 2011-11-04
  • 打赏
  • 举报
回复
7楼好强大!
zell419 2011-11-04
  • 打赏
  • 举报
回复
7楼好猛 。
曼尼叔叔 2011-11-04
  • 打赏
  • 举报
回复
我支持2楼,做一个实体类,把四个属性包含进去就行了撒,其他的交给业务类处理,何必要搞那么复杂呢
阿非 2011-11-04
  • 打赏
  • 举报
回复
序列化后你要做什么呢?

可以考虑DataSet.ReadXml

你可以看看读取后的效果,能不能满足你的需求。

头上记得加编码

<?xml version="1.0" encoding="gb2312" ?>

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧