如何使 xmlserializer 仅序列化纯 xml?

发布于 2024-08-12 03:47:17 字数 241 浏览 7 评论 0原文

我需要获取纯 xml,开头没有 xmlns:xsi="http:// /www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人问我粥可暖 2024-08-19 03:47:17

把这一切放在一起 - 这对我来说非常有效:

// To Clean XML
public string SerializeToString<T>(T value)
{
    var emptyNamespaces = 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, emptyNamespaces);
        return stream.ToString();
    }
}

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

// To Clean XML
public string SerializeToString<T>(T value)
{
    var emptyNamespaces = 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, emptyNamespaces);
        return stream.ToString();
    }
}
兰花执着 2024-08-19 03:47:17

使用 XmlSerializer.Serialize 方法重载,您可以在其中指定自定义命名空间并将其传递到此处。

var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
serializer.Serialize(xmlWriter, objectToSerialze, emptyNs);

传递 null 或空数组不会解决问题

Use the XmlSerializer.Serialize method overload where you can specify custom namespaces and pass there this.

var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
serializer.Serialize(xmlWriter, objectToSerialze, emptyNs);

passing null or empty array won't do the trick

◇流星雨 2024-08-19 03:47:17

您可以使用XmlWriterSettings并将属性OmitXmlDeclaration设置为true,如msdn 中描述。然后使用 XmlSerializer.Serialize(xmlWriter, objectToSerialize) 如此处所述

You can use XmlWriterSettings and set the property OmitXmlDeclaration to true as described in the msdn. Then use the XmlSerializer.Serialize(xmlWriter, objectToSerialize) as described here.

烟─花易冷 2024-08-19 03:47:17

这会将 XML 写入文件而不是字符串。对象票证是我正在序列化的对象。

使用的命名空间:

using System.Xml;
using System.Xml.Serialization;

代码:

XmlSerializerNamespaces emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

XmlSerializer serializer = new XmlSerializer(typeof(ticket));

XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true,
    OmitXmlDeclaration = true
};

using (XmlWriter xmlWriter = XmlWriter.Create(fullPathFileName, settings))
{
    serializer.Serialize(xmlWriter, ticket, emptyNamespaces); 
}

This will write the XML to a file instead of a string. Object ticket is the object that I am serializing.

Namespaces used:

using System.Xml;
using System.Xml.Serialization;

Code:

XmlSerializerNamespaces emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

XmlSerializer serializer = new XmlSerializer(typeof(ticket));

XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true,
    OmitXmlDeclaration = true
};

using (XmlWriter xmlWriter = XmlWriter.Create(fullPathFileName, settings))
{
    serializer.Serialize(xmlWriter, ticket, emptyNamespaces); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文