2011-02-17 38 views
1

我有以下代碼將Object轉換爲XML並且工作正常。從Object獲取純XML

public static string ConvertObjectToXML(Object obj) 
    { 
     String XmlizedString = null; 
     MemoryStream memoryStream = new MemoryStream(); 
     XmlSerializer xs = null; 

      if (obj is DerivedClass2) 
      { 
       xs = new XmlSerializer(typeof(DerivedClass2)); 
      } 

     TextWriter w = new StringWriter(); 
     //this.s = new XmlSerializer(this.type); 
     xs.Serialize(w, notoficationOrder); 
     w.Flush(); 
     //return w; 
     XmlizedString = w.ToString(); 
     w.Close(); 
     return XmlizedString.Trim(); 
    } 

,並讓下面的輸出

<?xml version="1.0" encoding="utf-16"?>* 
<Obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <List> 
     <!--...--> 
    </List> 
</Obj> 

但我不希望XML描繪XML命名空間XD等我需要的下面只有純粹的對象輸出

<Obj> 
    <List> 
    <!--...--> 
    </List> 
</Obj> 

感謝

海洋

+0

可能的重複問題:http://stackoverflow.com/questions/625927/omitting-all-xsi-and-xsd-namespaces-when-serializing-an-object-in-net – 2011-02-17 19:02:16

回答