我正在使用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。
如何反序列化包含实际标记中的数据的节点&lt; .....&gt;?我班上的财产会是什么样子?
更新 遵循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()]
答案 0 :(得分:1)
我认为你很亲密。这个结构对我有用。
您可以在以下其他问题中找到更多信息:
XmlSerializer - Deserialize different elements as collection of same element
How to Deserialize XML document
[XmlRoot("Item")]
public class ParentNodeName
{
[XmlElement("TransactionDate")]
public string TransactionData { get; set; }
[XmlElement("Product")]
public Product MyProduct { get; set; }
[XmlElement("Warning")]
public Warning MyWarning { get; set; }
}
public class Product
{
[XmlAttribute("version")]
public string Version { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
}
public class Warning
{
[XmlAttribute("message")]
public string Message { get; set; }
}
class Program
{
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(ParentNodeName));
FileStream stream = new FileStream(@"FakeData.xml", FileMode.Open);
var item = (ParentNodeName)serializer.Deserialize(stream);
}
}
FakeData.xml的内容如下所示:
<Item>
<TransactionDate>someDate</TransactionDate>
<Product version="someVersion" name="someName" />
<Warning message="someMessage" />
</Item>