views:

101

answers:

1

Hi. I want to generate an XML like this:

<a:foo>
  <b:bar><b:value="test" /></b:bar>
</a:foo>

I'm generating an XML from a class descriptor. I have this:

[Serializable]
[XmlType(Namespace = Constants.NS_A)]
[XmlRoot("Foo", Namespace = Constants.NS_A, IsNullable = false)]
public class Foo 
{
  private Bar_ bar = new Bar_();

  [XmlElementAttribute("bar")]
  public Bar_ Bar { get { return bar; } 
                    set { bar = value; } }

}

[Serializable]
[XmlType(Namespace = Constants.NS_B)]
[XmlRoot("Bar", Namespace = Constants.NS_B, IsNullable = false)]                        
  public class Bar_ 
  {                     
    private string value_;

    [XmlElementAttribute("value")]
    public string Value_
    {
        get
        {
            return value_;
        }
        set
        {
            value_ = value;
        }
    }
  }

With these classes I can generate an XML like this:

<a:foo>
  <a:bar><b:value="test" /></a:bar>
</a:foo>

And that's not what I want...

Little Help needed. Thanks

+1  A: 

You can specify the namespace that you want for your result XML in the XmlElementAttribute at the property Bar:

[Serializable] 
[XmlType(Namespace = Constants.NS_A)] 
[XmlRoot("Foo", Namespace = Constants.NS_A, IsNullable = false)]
public class Foo  
{ 
  private Bar_ bar = new Bar_(); 

  [XmlElementAttribute("bar", Namespace = Constants.NS_B)] 
  public Bar_ Bar { get { return bar; }  
                    set { bar = value; } } 

} 
EFrank