user1619644
user1619644

Reputation: 11

How do I declare namespace prefix for attribute in .NET?

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.

Upvotes: 1

Views: 2076

Answers (3)

Xavier Egea
Xavier Egea

Reputation: 4763

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.

Upvotes: 0

PawanS
PawanS

Reputation: 7193

    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;
    }
}

Upvotes: 1

detaylor
detaylor

Reputation: 7280

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?

Upvotes: 0

Related Questions