C#与Xml序列化

lv_ladynannan 2012-04-17 01:39:56
public class Request
{
[XmlArrayItem(ElementName = "fieldtype", IsNullable = true)]
[XmlArrayItem(ElementName = "field", IsNullable = true)]
[XmlArray]
public string[] requestkind { get; set; }
//[XmlArray]
public string[] field { get; set; }
public string[] fieldtype { get; set; }
public string[] medtype { get; set; }
public string[] patient { get; set; }
}
怎么才能序列化成如下的xml文档呢?序列化我会,但是不知道怎么嵌套,
<Request>
<requestkind>
<field>
<fieldtype>
<medtype/>
</fieldtype>
</field>
<field>
<fieldtype>
<medtype/>
</fieldtype>
</field>
</requestkind>
<requestkind>
<field>
<fieldtype>
<medtype/>
</fieldtype>
</field>
<field>
<fieldtype>
<medtype/>
</fieldtype>
</field>
</requestkind>
<requestkind>
<field>
<fieldtype>
<medtype/>
</fieldtype>
</field>
<field>
<fieldtype>
<medtype/>
</fieldtype>
</field>
</requestkind>
</Request>
请各位帮帮忙吧,谢谢!!!
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2012-04-17
  • 打赏
  • 举报
回复
本身格式没有嵌套,那么直接用 linq2xml 或者直接用 XmlDocument 生成。

你最好给出sample xml和测试数据,很难猜想 requestkind, field, fieldtype 怎么一一对应并嵌套的。
orochiheart 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

你这种需求,只能通过继承IXmlSerializable接口,自己实现WriteXml和ReadXml方法来做。
[/Quote]
+1 高手!
qldsrx 2012-04-17
  • 打赏
  • 举报
回复
你这种需求,只能通过继承IXmlSerializable接口,自己实现WriteXml和ReadXml方法来做。
lv_ladynannan 2012-04-17
  • 打赏
  • 举报
回复
中间的实现过程不会主要是
lv_ladynannan 2012-04-17
  • 打赏
  • 举报
回复
序列化XmlSerializer我知道,关键不知道,requestkind、field、fieldtype、medtype这些字段怎么嵌套到里面的,对类Request序列化这个我知道
bdmh 2012-04-17
  • 打赏
  • 举报
回复
我的代码,UpdateConfig 就是和你的 Request类类似

public static UpdateConfig LoadConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(UpdateConfig));
if (File.Exists(file))
{
StreamReader sr = new StreamReader(file);
UpdateConfig config = (UpdateConfig)xs.Deserialize(sr);
sr.Close();
return config;
}
else
{
return null;
}
}

public void SaveConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(UpdateConfig));
StreamWriter sw = new StreamWriter(file);
xs.Serialize(sw, this);
sw.Close();
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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