2008-11-03 118 views

回答

113

啊......沒關係。在提出問題後得出答案後,總是進行搜索。我正在序列化的對象是obj並且已被定義。用一個空的名稱空間添加一個XMLSerializerNamespace到集合中就可以了。

在VB中像這樣:

Dim xs As New XmlSerializer(GetType(cEmploymentDetail)) 
Dim ns As New XmlSerializerNamespaces() 
ns.Add("", "") 

Dim settings As New XmlWriterSettings() 
settings.OmitXmlDeclaration = True 

Using ms As New MemoryStream(), _ 
    sw As XmlWriter = XmlWriter.Create(ms, settings), _ 
    sr As New StreamReader(ms) 
xs.Serialize(sw, obj, ns) 
ms.Position = 0 
Console.WriteLine(sr.ReadToEnd()) 
End Using 

在C#這樣的:

//Create our own namespaces for the output 
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 

//Add an empty namespace and empty value 
ns.Add("", ""); 

//Create the serializer 
XmlSerializer slz = new XmlSerializer(someType); 

//Serialize the object with our own namespaces (notice the overload) 
slz.Serialize(myXmlTextWriter, someObject, ns); 
+8

我嘗試這樣做在VB,將xsi和xsd屬性消失但如XMLNS屬性:Q12 =,d3p1:類型,和xmlns:d3p1出現。 – MiddleKay 2013-03-04 09:40:03

+9

我試過C#版本,它刪除了xsi和xsd,但添加了q1的前綴:添加到所有XML標籤名稱中,這是我不想要的。它看起來像C#示例是不完整的,引用myXmlTextWriter,我認爲需要像VB示例一樣進行初始化。 – redtetrahedron 2013-05-31 15:33:51

+0

@redtetrahedron你有沒有找到擺脫`q1`垃圾的方法? – crush 2018-01-11 00:43:49

16

如果你想擺脫多餘的xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema",但仍保持自己的命名空間xmlns="http://schemas.YourCompany.com/YourSchema/",你使用相同的代碼如上述,除了這個簡單的變化:

// Add lib namespace with empty prefix 
ns.Add("", "http://schemas.YourCompany.com/YourSchema/"); 
4

如果您無法擺脫額外的xmlns的每個元素的屬性,從生成的類序列化到XML(如:當使用XSD.EXE)時,讓你有這樣的:

<manyElementWith xmlns="urn:names:specification:schema:xsd:one" /> 

然後我將與你分享我什麼工作(以前的答案的組合和我發現了什麼here

明確設置你的所有不同的xmlns如下:

Dim xmlns = New XmlSerializerNamespaces() 
xmlns.Add("one", "urn:names:specification:schema:xsd:one") 
xmlns.Add("two", "urn:names:specification:schema:xsd:two") 
xmlns.Add("three", "urn:names:specification:schema:xsd:three") 

然後將它傳遞給序列化

serializer.Serialize(writer, object, xmlns); 

你將不得不在根元素中聲明三個名稱空間並沒有更多的需要,這將在相應前綴

<root xmlns:one="urn:names:specification:schema:xsd:one" ... /> 
    <one:Element /> 
    <two:ElementFromAnotherNameSpace /> ... 
其它元件中產生
5

我的建議是輔助類:

public static class Xml 
{ 
    #region Fields 

    private static readonly XmlWriterSettings WriterSettings = new XmlWriterSettings {OmitXmlDeclaration = true, Indent = true}; 
    private static readonly XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces(new[] {new XmlQualifiedName("", "")}); 

    #endregion 

    #region Methods 

    public static string Serialize(object obj) 
    { 
     if (obj == null) 
     { 
      return null; 
     } 

     return DoSerialize(obj); 
    } 

    private static string DoSerialize(object obj) 
    { 
     using (var ms = new MemoryStream()) 
     using (var writer = XmlWriter.Create(ms, WriterSettings)) 
     { 
      var serializer = new XmlSerializer(obj.GetType()); 
      serializer.Serialize(writer, obj, Namespaces); 
      return Encoding.UTF8.GetString(ms.ToArray()); 
     } 
    } 

    public static T Deserialize<T>(string data) 
     where T : class 
    { 
     if (string.IsNullOrEmpty(data)) 
     { 
      return null; 
     } 

     return DoDeserialize<T>(data); 
    } 

    private static T DoDeserialize<T>(string data) where T : class 
    { 
     using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(data))) 
     { 
      var serializer = new XmlSerializer(typeof (T)); 
      return (T) serializer.Deserialize(ms); 
     } 
    } 

    #endregion 
} 

:)

7

如果您想刪除命名空間,您可能還想刪除該版本,爲了節省您的搜索我已經添加了該功能,以便下面的代碼可以執行這兩個操作。

我還用普通的方法將它包裹起來,因爲我創建了非常大的xml文件,這些文件太大而無法在內存中序列化,所以我打斷了我的輸出文件並將其序列化爲較小的「塊」:

public static string XmlSerialize<T>(T entity) where T : class 
    { 
     // removes version 
     XmlWriterSettings settings = new XmlWriterSettings(); 
     settings.OmitXmlDeclaration = true; 

     XmlSerializer xsSubmit = new XmlSerializer(typeof(T)); 
     using (StringWriter sw = new StringWriter()) 
     using (XmlWriter writer = XmlWriter.Create(sw, settings)) 
     { 
      // removes namespace 
      var xmlns = new XmlSerializerNamespaces(); 
      xmlns.Add(string.Empty, string.Empty); 

      xsSubmit.Serialize(writer, entity, xmlns); 
      return sw.ToString(); // Your XML 
     } 
    } 
相關問題