To set the namespace of the attribute, you'll need to use the Namespace
property of XmlAttributeAttribute
.
If it's particularly important that the prefix used for that namespace is "x" then you can control this using the XmlSerializerNamespaces
class when doing serialization, optionally with XmlNamespaceDeclarationsAttribute
.
Here's a working example:
[XmlRoot(Namespace = "http://foo")]
public class MyClass
{
private XmlSerializerNamespaces xmlns;
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns
{
get
{
if (xmlns == null)
{
xmlns = new XmlSerializerNamespaces();
xmlns.Add("x", "http://xxx");
}
return xmlns;
}
set { xmlns = value; }
}
[XmlAttribute("uid", Namespace = "http://xxx")]
public int Uid { get; set; }
}
class Program
{
static void Main(string[] args)
{
var s = new XmlSerializer(typeof(MyClass));
s.Serialize(Console.Out, new MyClass { Uid = 123 });
Console.ReadLine();
}
}
Which produces:
<?xml version="1.0" encoding="ibm850"?>
<MyClass
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:x="http://xxx"
x:uid="123"
xmlns="http://foo"/>