啊…永远不会。总是在问题提出后的搜索产生答案。我正在序列化的对象是obj,已经被定义。将一个具有单个空命名空间的XMLSerializerNamespace添加到集合中。
原文链接:https://www.f2er.com/xml/294069.html在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);