2

with the programming is this:

As a result I want to have this:

<rootprefix:rootname 
     noPrefix="attribute with no prefix"
     firstprefix:attrOne="first atrribute"
     secondprefix:attrTwo="second atrribute with different prefix">

     ...other elements...

 </rootprefix:rootname>

The way to do this by coding is:

NameTable nt = new NameTable();
nt.Add("key");

XmlNamespaceManager ns = new XmlNamespaceManager(nt);
ns.AddNamespace("firstprefix", "fp");
ns.AddNamespace("secondprefix", "sp");

root.SetAttribute("attrOne", ns.LookupPrefix("fp"), "1st attribute");
root.SetAttribute("attrTwo", ns.LookupPrefix("sp"), "2nd with different prefix");

But I want to do this using attributes of types above of the class declaration. For eg: [XmlType(Namespace = "bb:aaaa")] or something else.

How can I do this?

Edit: My class something like this:

[XmlRoot("Node", Namespace="http://flibble")]
public class MyType {
    [XmlElement("chileNode")]
    public string Value { get; set; }
}

And I want to have this result:

<?xml version="1.0" encoding="ibm857"?>
<myNamespace:Node xmlns:myNamespace="http://hede.com" />

Without writing this code:

static class Program
{
    static void Main()
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("myNamespace", "http://hede.com");
        XmlSerializer xser = new XmlSerializer(typeof(MyType));
        xser.Serialize(Console.Out, new MyType(), ns);
    }
}

With some attribute like this:

[XmlRoot("Node", Namespace="http://hede.com", NamespacePrefix="myNamespace")]
public class MyType {
    [XmlElement("chileNode")]
    public string Value { get; set; }
}

But I couldn't find a way putting "myNamespace" prefix in front of xml tag.

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
uzay95
  • 16,052
  • 31
  • 116
  • 182
  • I think those prefixes are called namespaces. – Reactgular Nov 21 '13 at 13:42
  • possible duplicate of [How can I serialize a class with an attribute](http://stackoverflow.com/questions/688985/how-can-i-serialize-a-class-with-an-attribute) – Reactgular Nov 21 '13 at 13:44
  • I think I couldn't tell my problem. I'm gonna edit it now. – uzay95 Nov 21 '13 at 14:42
  • 2
    @MathewFoscarini The prefixes aren't called namespaces--they're called namespace prefixes. The namespace is the URI to which the prefix refers. – Brian Warshaw Nov 21 '13 at 14:50
  • You are right and want to put prefix inside attribute declaration like namespace uri declaration that we did. – uzay95 Nov 21 '13 at 14:53
  • possible duplicate of [XML Serialization and namespace prefixes](http://stackoverflow.com/questions/2339782/xml-serialization-and-namespace-prefixes) – Alex Filipovici Nov 21 '13 at 15:22
  • @AlexFilipovici, I took the example codes from that question. Question is different because I'm asking the solution which is doing by "attributes" not coding. I just want to create my classes and mark their xml output using by attributes... I hope this time my question can understandable. – uzay95 Nov 21 '13 at 21:05

0 Answers0