Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
I think those prefixes are called namespaces. –  Mathew Foscarini Nov 21 '13 at 13:42
    
possible duplicate of How can I serialize a class with an attribute –  Mathew Foscarini 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.