2010-04-28 14 views
1

我已經和web服務的WebMethod......可能不會在這個上下文中使用......而系列化

[webMethod] 
[XMLInclude(typeof(ContractCar[])) 
public object GetObjects(Cars[] cars) 
{ 
return Translator.ToObjects(Facade.GetObjects(cars); 

} 

public static object GetObjects(Cars cars) 
{ 
List<Car> cars =new List<Country(...fillingcollection) 
return cars.ToArray(), 
} 

public static object ToObjects(object collection) 
{ 
if(collection is Car[]) 
{ 
return ConvertModelCarsToContractCars(collection), 
} 

public ContractCar[] ConvertModelCarsToContractCars(Cars[] collection) 
{ 
...there is rewriting pool... 
} 

我在客戶側得到例外:

有錯誤產生XML文檔。

我正在使用此功能來檢查我將發送給客戶端的集合,它的工作原理。

public static void SerializeContainer(object obj) 
     { 
      try 
      { 
       // Make sure even the construsctor runs inside a 
       // try-catch block 
       XmlSerializer ser = new XmlSerializer(typeof(object)); 

       TextWriter w = new StreamWriter(@"c:\list.xml"); 


       ser.Serialize(w, obj); 
       w.Close(); 

      } 
      catch (Exception ex) 
      { 
       DumpException(ex); 
      } 
     } 

有趣的是,當集合只有一個元素[的WebMethod]工作正常,但是當它越brokes

回答

1

您需要指出的串行器和WSDL中,一個Cars可以是ModelCar使用XmlIncludeAttribute

[WebMethod] 
[XmlInclude(typeof(ModelCar))] 
public object GetObjects(Cars[] cars) 
{ 
    return Translator.ToObjects(Facade.GetObjects(cars); 
} 

你需要指出哪些是你從方法返回的可能類型。就像在方法簽名中你要返回的對象一樣,序列化程序不知道你在運行時返回的實際類型。

而在你的系列化例如:

XmlSerializer ser = new XmlSerializer(
    typeof(object), 
    new Type[] { 
     // List all the possible types here that you are using 
     typeof(Foo), 
     typeof(Bar) 
    } 
); 
+0

我仍然有錯誤:ModelCar []可能不會在此上下文中使用... – user278618 2010-04-28 14:51:27

相關問題