本文介绍了我怎样才能让XmlSerializer的唯一序列纯XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要纯XML,而不< XML版本=1.0编码=UTF-16> 开头和的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema的XmlSerializer 第一要素。我该怎么办呢?

I need to get plain xml, without the <?xml version="1.0" encoding="utf-16"?> at the beginning and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" in first element from XmlSerializer. How can I do it?

推荐答案

要把这一切在一起 - 这完美的作品对我来说:

To put this all together - this works perfectly for me:

    // To Clean XML
    public string SerializeToString(T value)
    {
        var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
        var serializer = new XmlSerializer(value.GetType());
        var settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;

        using (var stream = new StringWriter())
        using (var writer = XmlWriter.Create(stream, settings))
        {
            serializer.Serialize(writer, value, emptyNamepsaces);
            return stream.ToString();
        }
    }

这篇关于我怎样才能让XmlSerializer的唯一序列纯XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:49