1

The problem is similar to this:

How do I specify XML serialization attributes to support namespace prefixes during deserialization in .NET?

but specifically for attribute. I have something like:

<person xmlns:a="http://example.com" xmlns:b="http://sample.net"> <a:fName a:age="37">John</a:fName> <b:lName>Wayne</b:lName> </person>

and I can't find a way to put the prefix to the attribute "age".

How the solution proposed in the link above must be changed to reach the goal? I tried different solution without success.

2
  • 1
    You consider people to be ageless, but there first names to have an age? Interesting... Commented Aug 23, 2012 at 12:35
  • @Damien_The_Unbeliever ha ha!! you are right, but might be he gave pseudo XML that suits his requirement. Commented Aug 23, 2012 at 13:59

3 Answers 3

1
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;

    namespace XMLSer
    {
        class Program
        {
            static void Main(string[] args)
            {
                FName fname = new FName { Age = 16.5, Text = "John" };

            Person person = new Person();

            person.fname = fname;
            person.lname = "Wayne";

            XmlSerializer ser = new XmlSerializer(typeof(Person));
            ser.Serialize(Console.Out, person);
            Console.ReadKey();
        }
    }

    [XmlRoot(ElementName = "person")]
    public class Person
    {
        [XmlElement(Namespace = "http://example.com")]
        public FName fname;

        [XmlElement(Namespace = "http://sample.com")]
        public string lname;

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

        public Person()
        {
            xmlns.Add("a", "http://example.com");
            xmlns.Add("b", "http://sample.com");
        }
    }

    public class FName
    {
        [XmlAttribute("age")]
        public double Age;

        [XmlText]
        public string Text;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for the answer but your code generate the following xml: <?xml version="1.0" encoding="utf-8"?> <person xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" xmlns:a="example.com" xmlns:b="sample.com"> <a:fname age="16.5">John</a:fname> <b:lname>Wayne</b:lname> </person> and unfortunatly "age" still does not contain the prefix.
0

You should be able to do the same as in the linked example (but using the XmlAttributeAttribute instead of the XmlElementAttribute). Your property declaration would be something like:

[XmlAttribute(Namespace = "http://example.com")]
public decimal Age { get; set; }

Further details and examples for the XmlAttributeAttribute are on the msdn site.

To get the attribute on the fName element I think that you would need the age to be a property of the first name property. Is the attribute supposed to be on the fName element or on the person element?

1 Comment

I tried to insert the namespace but the result is the same. No prefix on the attribute.
0

I had the same problem trying to specify "xsi:schemaLocation" as an attribute.

I fixed it doing the next:

[XmlElement("xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Xsi { get; set; }    

[XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string SchemaLocation { get; set; }

Note: Both NameSpaces must match.

Comments

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.