XmlSerialization和属性属性 [英] XmlSerialization and attributes on properties

查看:145
本文介绍了XmlSerialization和属性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,

非常简单,我有一些非常简单的XML,可以序列化和反序列化.

so simply enough I have some very simple XML that I am serializing and deserializing.

<data>
  <item name="name">
    <value profile="default">my value</value>
  </item>
</data>

现在,通常我们将Data设置为具有Item类型的属性的可序列化类. ItemClass将具有一些标记为属性的属性.

Now, typically we'd make Data a serializable class with a property of type Item.  The ItemClass would have several properties some flagged as an attribute.

[XmlRoot("data")]
public class Data{
  public ItemClass item {get; set;}
}

public class ItemClass {
  [XmlAttribute("name")]
  public string Name {get; set;}
  [XmlElement("value')]
  public string Value {get; set;}
}

不过,您会看到Xml元素值上有一个名为"profile"的属性.

However as you can see there is an attribute on the Xml Element Value named "profile".

我的问题:是否有办法在ItemClass上拥有一个属性,即配置文件"属性?属性值的属性.

My question: is there a way to have a property on ItemClass that would be the "profile" attribute on the value property/element. 

public class ItemClass {
  [XmlAttribute("name")]
  public string Name {get; set;}
  [XmlElement("value")]
  public string Value {get; set;}
  [XmlAttribute(Element = "value", Name = "profile")]
  public string Profile {get; set;}
}

是使用IXmlserializable或通过使IXmlserializable实现价值"的唯一途径.一个带有属性和文本节点的类?

Is the only way to achieve the through either use IXmlserializable or to make "value" a class with an attribute and a Text node?

谢谢

Jaeden"Sifo Dyas" al'Raec Ruiner

Jaeden "Sifo Dyas" al'Raec Ruiner

永远不要信任计算机.您的大脑比任何微芯片都要聪明."
PS-不要在其他人的问题上标记答案.诸如假期和假期之类的事情可能会减少及时的活动,并且直到提出问题的人能够测试您的答案之前,仅因为您认为正确,这是不正确的.标记它 为他们正确设置通常会阻止其他人阅读问题,甚至可能无法提供真正的正确"配置.答案.

"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.

推荐答案

嗨JaedenRuiner,

Hi JaedenRuiner,

根据您的描述,根据我的经验,我们可以创建一个

According to your description, from my experience may be we can Create a class which has

元素和名称.代码如下所示:

a Element and name. Code  shown below:

namespace ConsoleApplication1
{
    [XmlRoot("data")]
    public class Data
    {
        public ItemClass item {get; set;}
    }

    public class ItemClass
    {
        [XmlAttribute("name")]
         public string Name {get; set;}
        [XmlText]
        public ElementWithAttribute value { get; set; }
    }    

    public class ElementWithAttribute
    {
        [XmlAttribute]
        public string profile { get; set; }
        [XmlText]
        public string My_Value{ get; set; }
    }   
}

有关更多信息,请参阅以下文章:

For more information, please refer to the following articles:

1. .

1.XmlAttributes.XmlAttribute Property

希望对您有所帮助.

最好的问候,

格雷迪


这篇关于XmlSerialization和属性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆