7

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'.

This is what i have currently:

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

This is what i would like to generate:

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

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.

0

2 Answers 2

8

You can use a combination of XMLAttribute and XmlText()

take below example of class declaration:

    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>

Sign up to request clarification or add additional context in comments.

Comments

4

It is possible if you define another type as below:

public class Person
{

    private string _name;


    [XmlIgnore]
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            ThePersonName = new PersonName()
                                {
                                    Name = FullName,
                                    NiceName = _name
                                };
        }
    }

    [XmlElement(ElementName = "Name")]
    public PersonName ThePersonName { get; set; }

    public string FullName { get; set; }

}

public class PersonName
{
    [XmlAttribute]
    public string NiceName { get; set; }

    [XmlText]
    public string Name { get; set; }
}

Using

        XmlSerializer s = new XmlSerializer(typeof(Person));
        Person ali = new Person();
        ali.FullName = "Ali Kheyrollahi";
        ali.Name = "Nobody";
        s.Serialize(new FileStream("ali.xml",FileMode.Create), ali);

Will generate

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name NiceName="Nobody">Ali Kheyrollahi</Name>
  <FullName>Ali Kheyrollahi</FullName>
</Person>

3 Comments

Thanks for the answers however what i'm trying to do is code an alternative for the node name not the persons name. I know its probably better to write this into the xslt its just i want to manage it from .net rather then the xslt. What i mean is i need to generate something like <FullName NiceName="Full Name">Mr Joe Blogs</FullName> In my Xslt i would then just output the nicename for each element and the value to produce an much nicer user interface. like "Full Name=Mr Joe Blogs" instead of "FullName=Joe Blogs" or an alternative is <TEL nicename="telephone">1234</TEL>
I can't figure out how to apply a sibling level property to another property as an attribute. I think Aliostad's answer "seems" a bit messy but I think it's the way to go.
You are better off doing this in xslt, srsly. XmlSerializer can't handle this sort of contortionism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.