如何在不得到的xmlns序列化对象到XML =" ..."?对象、序列化、如何在、QUOT

由网友(點指゛溫柔)分享简介:有没有办法对我来说没有XML命名空间也自动序列化到序列化.NET对象?如此看来,在默认情况下.NET相信XSI和XSD的命名空间应包括在内,但我不想让他们在那里。Is there a way for me to serialize an object in .NET without the XML Namespace...

有没有办法对我来说没有XML命名空间也自动序列化到序列化.NET对象?如此看来,在默认情况下.NET相信XSI和XSD的命名空间应包括在内,但我不想让他们在那里。

Is there a way for me to serialize an object in .NET without the XML Namespaces automatically serializing also? It seems that by default .NET believes the XSI and XSD namespaces should be included, but I don't want them there.

推荐答案

啊......没关系。它总是后问题,提出了得到答案的搜索。我正被序列化对象是 OBJ 并已确定。添加XMLSerializerNamespace用一个空的命名空间集合是卓有成效的。

Ahh... nevermind. It's always the search after the question is posed that yields the answer. My object that is being serialized is obj and has already been defined. Adding an XMLSerializerNamespace with a single empty namespace to the collection does the trick.

在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#这样的:

in C# like this:

//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);
阅读全文

相关推荐

最新文章