请问xml反序列化的一个类成员设置问题

renmms 2011-04-18 12:01:27

<Data>
<fields name = "t1">v1</fields>
<fields name = "t2">v2</fields>
<fields name = "t3">v3</fields>
<fields name = "t4">v4</fields>
</Data>


请问如何设置类fileds 来取name和value的值?即:t1, v1 ...


Class fileds
{
[XmlAttribute("name")]
public string name {get;set;}

// 问题处:
public string value{get;set;}
}


name可以取到,但是value就取不到,value 上面的属性到底该怎么写?XmlAttribute or XmlElement ?
...全文
42 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
renmms 2011-04-18
  • 打赏
  • 举报
回复
Up一下,我没有说明白么?所有的测试代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication4
{
[XmlRoot("Data")]
public class Data
{
[XmlElement("field")]
public List<Field> fields { get; set; }
}

public class Field
{
[XmlAttribute("name")]
public string name { get; set; }

[XmlAttribute("field")]
public string value { get; set; }
}

class Program
{
static void Main(string[] args)
{
Data data = null;
XmlSerializer serializer = new XmlSerializer(typeof(Data));
using (TextReader textReader = new StreamReader("1.xml"))
{
data = (Data)serializer.Deserialize(textReader);
}
}
}
}



1.XML

<?xml version="1.0" encoding="utf-8" ?>
<Data>
<field name = "t1">v1</field>
<field name = "t2">v2</field>
<field name = "t3">v3</field>
<field name = "t4">v4</field>
</Data>
renmms 2011-04-18
  • 打赏
  • 举报
回复
自己解决,原来是 [XmlText] 属性

110,533

社区成员

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

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

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