Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

c# - Removing Wrapper Elements from XML-Serialized Array

I am using VSTS2008 + C# + .Net 3.0. I am using below code to serialize XML, and my object contains array type property, but there some additional elements' layer (in my sample, MyInnerObject and MyObject) generated which I want to remove from the generated XML file. Any ideas?

Current generated XML file,

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

Expected XML file,

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

Current code,

public class MyClass
{
    private MyObject[] _myObjectProperty;

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

    [XmlArrayItemAttribute(IsNullable = false)]
    public MyInnerObject[] MyInnerObjectProperty
    {
        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].MyInnerObjectProperty = new MyInnerObject[1];
        instance.MyObjectProperty[0].MyInnerObjectProperty[0] = new MyInnerObject();
        instance.MyObjectProperty[0].MyInnerObjectProperty[0].ObjectName = "Foo Type";
        s.Serialize(fs, instance);

        return;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of

[XmlArrayItemAttribute]

use:

[XmlElement]

To figure this out in the future, you can run (from a VS Command Prompt):

xsd.exe test.xml
xsd.exe /classes test.xsd

This generates test.cs, that contains the xml serializable class, based on the xml. This works even better if you have an .xsd around ofcourse


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...