1

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
  • 2
    <b:value="test" /> is not a valid XML. Try to be more precise in what you are expecting as a result. Commented Jan 26, 2010 at 15:54

1 Answer 1

2

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

} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.