C#-具有属性和节点值的Xml元素 [英] C# - Xml Element with attribute and node value
本文介绍了C#-具有属性和节点值的Xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些Xml需要反序列化为一个对象.Xml是:
I have some Xml that I need to deserialize into an object. The Xml is:
<Person>
<Type id="1234">Bob</Type>
</Person>
和类是:
public class Person { public Type Type; }
public class Type {
[XmlAttribute("id")]
public string id;
// another property for value "Bob" here, such as:
public string value; // ????
}
我想使用 XmlSerializer.Deserialize
反序列化此Xml到上面的具体对象中(避免使用XPath等)
I'd like to deserialize this Xml using XmlSerializer.Deserialize
, into the concrete objects above (avoiding using XPath, etc.)
我可以使用哪种Xml属性来装饰"Type"类,以便不仅具有"id"属性而且还具有值("Bob")?
What Xml attribute can I decorate the "Type" class with so that I have not only an "id" attribute but also a value ("Bob")?
推荐答案
您将必须添加一个属性,如
You would have to add a property like
[XmlText]
public string Text;
这篇关于C#-具有属性和节点值的Xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文