3

I am using VSTS2008 + C# + .Net 3.0. I am using below code to serialize XML, here is my current code and serialized XML file. My purpose is I want to make MyInnerObjectProperties belongs to a special XML namespace (http://foo/2009) and making this namespace as default namespace. Any ideas how to implement this?

Current output:

<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyObjectProperty>
    <MyInnerObjectProperties>
      <MyInnerObjectProperty>
        <ObjectName>Foo Type</ObjectName>
      </MyInnerObjectProperty>
      <MyInnerObjectProperty>
        <ObjectName>Goo Type</ObjectName>
      </MyInnerObjectProperty>
    </MyInnerObjectProperties>
  </MyObjectProperty>
</MyClass>

Current code:

public class MyClass
{
    private MyObject[] _myObjectProperty;

    [XmlElement(IsNullable=false)]
    public MyObject[] MyObjectProperty
    {
        get
        {
            return _myObjectProperty;
        }
        set
        {
            _myObjectProperty = value;
        }
    }
}
public class MyObject
{
    private MyInnerObject[] _myInnerObjectProperty;

    [XmlArrayItemAttribute("MyInnerObjectProperty", typeof(MyInnerObject),  IsNullable=false)]
    public MyInnerObject[] MyInnerObjectProperties
    {
        get
        {
            return _myInnerObjectProperty;
        }
        set
        {
            _myInnerObjectProperty = value;
        }
    }
}

public class MyInnerObject
{
    public string ObjectName;
}

public class Program
{
    static void Main(string[] args)
    {
        XmlSerializer s = new XmlSerializer(typeof(MyClass));
        FileStream fs = new FileStream("foo.xml", FileMode.Create);
        MyClass instance = new MyClass();
        instance.MyObjectProperty = new MyObject[1];
        instance.MyObjectProperty[0] = new MyObject();
        instance.MyObjectProperty[0].MyInnerObjectProperties = new MyInnerObject[2];
        instance.MyObjectProperty[0].MyInnerObjectProperties[0] = new MyInnerObject();
        instance.MyObjectProperty[0].MyInnerObjectProperties[0].ObjectName = "Foo Type";
        instance.MyObjectProperty[0].MyInnerObjectProperties[1] = new MyInnerObject();
        instance.MyObjectProperty[0].MyInnerObjectProperties[1].ObjectName = "Goo Type";

        s.Serialize(fs, instance);

        return;
    }
}
3
  • 1
    George, when did you stop using .NET 3.5 SP1? Commented Aug 4, 2009 at 15:22
  • John, I am working on several projects. Some of them are developed from scrach, and I will use .Net 3.5 SP1 + .Net 2.0 SP2. For legacy projects, I will keep its original .Net version. It is a pity that LINQ cannot be used in .Net 3.0 based projects. Any way, any ideas for my question? Commented Aug 4, 2009 at 15:29
  • I'm working on it. Someone will have the answer before me, I'm sure. Commented Aug 4, 2009 at 15:40

3 Answers 3

2

How about this:

[XmlArrayItemAttribute( Namespace = "http://foo.com/2009" /* other attr. params. */ )]
public MyInnerObject[] MyInnerObjectProperties
{
    get { ... }
    set { ... }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try

public class MyObject
{
    [XmlArrayItemAttribute("MyInnerObjectProperty", typeof (MyInnerObject),
        IsNullable = false)]
    [XmlArray(Namespace = "http://foo.com/2009")]
    public MyInnerObject[] MyInnerObjectProperties { get; set; }
}

for me, this produces:

<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <MyObjectProperty>
        <MyInnerObjectProperties xmlns="http://foo.com/2009">
            <MyInnerObjectProperty>
                <ObjectName>Foo Type</ObjectName>
            </MyInnerObjectProperty>
            <MyInnerObjectProperty>
                <ObjectName>Goo Type</ObjectName>
            </MyInnerObjectProperty>
        </MyInnerObjectProperties>
    </MyObjectProperty>
</MyClass>

Comments

1

You need to create an XmlSerializerNamespaces object, and add your needed namespaces to it.

The XmlSerializerNamespaces object contains the XML namespaces and prefixes that the XmlSerializer uses to generate qualified names in an XML-document instance.

In your c# code:

XmlSerializerNamespaces myNameSpaces = new XmlSerializerNamespaces();
myNameSpaces.Add("MyInnerObject", "http://foo/2009");

Then, add an attribute to your class, like this:

public class MyInnerObject
{
[XmlElement(Namespace = "http://foo/2009")]

More info at:

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx

2 Comments

Thanks Robert. My purpose is not adding everything of class MyClass into a unified namespace, but to add just MyInnerObjectProperties elements into namespace "foo/2009". I think your solution will add all elements from class MyClass into "foo/2009", correct? Any ideas to my issue?
You don't have to use it for the whole class. You can pick and choose the elements and attributes you want. See msdn.microsoft.com/en-us/library/…

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.