2009-08-04 34 views
8

Tôi đang sử dụng VSTS2008 + C# + .Net 3.0. Tôi đang sử dụng mã dưới đây để tuần tự hóa XML, và đối tượng của tôi chứa thuộc tính kiểu mảng, nhưng có một số lớp phần tử bổ sung (trong mẫu của tôi, MyInnerObject và MyObject) được tạo mà tôi muốn loại bỏ khỏi tệp XML được tạo ra. Bất kỳ ý tưởng?Xóa các phần tử trình bao bọc từ mảng được sắp xếp theo XML

hiện tại tạo ra tập tin XML,

<?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> 

mong đợi tập tin XML,

<?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> 

mã hiện tại,

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

Trả lời

14

Thay vì

[XmlArrayItemAttribute] 

sử dụng:

[XmlElement] 

Để con số này ra trong tương lai, bạn có thể chạy (từ một Prompt VS Command):

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

này tạo Test.cs, có chứa các lớp serializable xml, dựa trên xml. Điều này hoạt động tốt hơn nếu bạn có một .xsd quanh ofcourse

+0

Cảm ơn Sander, giải pháp của bạn hoạt động. Bạn có thể mô tả thêm một chút về lý do tại sao sử dụng XmlArrayItemAttribute tác động đến kết quả XML không? Và tại sao XmlElement hoạt động? – George2

+2

Chỉ có một sự khác biệt trong chúng, cho phép bạn sử dụng cả hai biểu diễn xml, XmlElement bỏ qua tên thuộc tính và chỉ xuất các phần tử, XmlArrayItem tạo một mảng (được đặt tên với tên đặc tính) của các phần tử (được đặt tên với tên bạn cung cấp) –

+0

Cảm ơn Sander , câu hỏi đã được trả lời. Tôi có thêm câu hỏi ở đây, đánh giá cao nếu bạn có thể trợ giúp, http://stackoverflow.com/questions/1227897/adding-an-additional-layer-to-xml-serialization-result-for-an-array – George2

Các vấn đề liên quan