【发布时间】:2013-11-21 18:22:42
【问题描述】:
我正在使用 XmlSerializer .Deserialize 编写反序列化程序。基本的反序列化程序有效,所以我不会用该代码混淆问题。我写了一个类来存储数据。我成功反序列化了一个节点,例如
<TransactionDate>2013-11-20T18:26:35.363</TransactionDate>
使用
public string TransactionDate { get; set; }
但是我将如何使这些节点脱轨以提取数据
<Product version="1.1.0" name="Product Name"/>
或
<Warning message="A Warning message here"/>
我正在使用现有服务,因此无法更改输入 xml 的格式。
我尝试过使用 XmlElement 属性
[XmlElement("Product version")]
public string Productversion { get; set; }
或
[XmlElement("Warning message="A Warning message here"/>)]
public List<string> Message { get; set; } }
第二个有转义字符问题,都返回null。
如何反序列化包含实际标签 <.....> 中的数据的节点?我班级的财产会是什么样子?
更新: 按照 fcuesta 的建议(不需要 XmlElement 属性,它没有任何区别)我尝试了
public class Product { public string version { get; set; } public string name { get; set; } }
成员仍然没有被填充并且为空。
更新 2:完整答案
杰森的回答奏效了。他的代码和当我将[XmlRoot()] 应用到我的代码时。但是 Xml 实际上是更加嵌套的,并且还有标签,其中数据位于标签中和标签之间(Answer 节点)。这是 Xml:
<PlatformResponse>
<Response>
<Questions>
<Question text="Which one of the following area codes is associated with you?" type="1">
<Answer correct="false">813</Answer><Answer correct="false">352</Answer>
<Answer correct="true">305/786</Answer><Answer correct="false">850</Answer>
<Answer correct="false">256</Answer><Answer correct="false">205</Answer>
<Answer correct="false">912</Answer><Answer correct="false">615</Answer><Answer correct="false">478</Answer>
<Answer correct="false">None of the above</Answer>
</Question>
<Question text="Which one of the following counties is associated with you?" type="2">
<Answer correct="false">Benton</Answer><Answer correct="true">Miami-Dade</Answer>
<Answer correct="false">Burke</Answer><Answer correct="false">Lafayette</Answer>
<Answer correct="false">Monroe</Answer><Answer correct="false">Dickson</Answer>
<Answer correct="false">Coosa</Answer><Answer correct="false">Smith</Answer>
<Answer correct="false">Muscogee</Answer><Answer correct="false">None of the above</Answer>
</Question>
<Question text="Which one of the following zip codes is associated with you?" type="3">
<Answer correct="false">33271</Answer>
<Answer correct="false">33929</Answer>
<Answer correct="false">33927</Answer>
<Answer correct="false">33007</Answer>
<Answer correct="true">33055</Answer>
<Answer correct="false">33061</Answer>
<Answer correct="false">33556</Answer>
<Answer correct="false">33263</Answer>
<Answer correct="false">33356</Answer>
<Answer correct="false">None of the above</Answer>
</Question>
</Response>
</PlatformResponse>
这是我的课程:
[XmlRoot("PlatformResponse")]
public class IDChckRspn
{
//public TransactionDetails TransactionDetails { get; set; }
public Response Response { get; set; }
}
public class Response
{
public Questions Questions { get; set; }
}
public class Questions
{
[XmlElement("Question")]
public List<Question> Question { get; set; }
}
public class Question
{
[XmlAttribute("text")]
public string Text { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlElement("Answer")]
public List<Answer> Answer { get; set; }
}
public class Answer
{
[XmlAttribute("correct")]
public string Correct { get; set; }
[XmlText()]
public string Text { get; set; }
}
这是我的反序列化器:
XmlSerializer s = new XmlSerializer(typeof(IDChckRspn));
StreamReader r = new StreamReader(@"c:\temp\response.xml");
object obj = s.Deserialize(r);
IDChckRspn _response = (IDChckRspn)obj;
这成功地反序列化了树和在标签中和标签之间具有数据的 Answer 标签。 注意:这是通过使用四种不同的属性来装饰各种类和成员来实现的。
[XmlRoot("PlatformResponse")] [XmlElement("Question")] [XmlAttribute("text")] [XmlText()]
【问题讨论】:
标签: c# xml xml-deserialization