XML属性添加到字符串属性 [英] Add Xml Attribute to string property

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

问题描述

我有有一个名为名称我想继续通过系列化同生成的XML,但属性添加到名为NiceName与全名的价值元素的字符串属性的自定义对象。

I have a custom object which has a string property called 'Name' I'd like to keep the XML generated by serialization the same but add an attribute to the element called 'NiceName' with a value of 'Full name'.

这是我目前:

<TheObject>
  <Name>mr nobody</Name>
</TheObject>

这是我想什么来生成:

<TheObject>
  <Name NiceName='Full name'>mr nobody</Name>
</TheObject>



我只需要这一段XSLT,所以我不想更改类工作,如果方式可能。 I.E.从字符串更改名称,自定义类。所有对象都会有它永远不会改变它会只完全被读取相同的属性。

I only need this for some XSLT so i don't want to change the way the class works if possible. I.E. Changing name from string to a custom class. All objects will have the same attribute it will never change it is going to be totally read only.

推荐答案

您可以使用组合XMLAttribute和XMLTEXT()

You can use a combination of XMLAttribute and XmlText()

取例如类的声明如下:

    public class Description {
    private int attribute_id;
    private string element_text;

    [XmlAttribute("id")]
    public int Id {
        get { return attribute_id; }
        set { attribute_id = value; }
    }

    [XmlText()]
    public string Text {
        get { return element_text; }
        set { element_text = value; }
    }
}



输出会

The output will be

<XmlDocRoot>
<Description id="1">text</Description>



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

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