如何使用 XMLSerializer 添加没有前缀的默认命名空间

发布于 2024-08-25 19:49:07 字数 1526 浏览 5 评论 0原文

我正在尝试使用 XmlSerializer 生成一个包含默认命名空间且没有前缀的 XML 文档,例如

<?xml version="1.0" encoding="utf-8" ?>
<MyRecord ID="9266" xmlns="http://www.website.com/MyRecord">
    <List>
        <SpecificItem>

使用以下代码...

string xmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, string.Empty);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, myRecord, xmlnsEmpty);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlizedString = this.UTF8ByteArrayToString(memoryStream.ToArray());

和类结构...

[Serializable]
[XmlRoot("MyRecord")]
public class ExportMyRecord
{
    [XmlAttribute("ID")]
    public int ID { get; set; }

现在,我尝试了各种选项.. .

XmlSerializer xs = new XmlSerializer
                     (typeof(ExportMyRecord),"http://www.website.com/MyRecord");

或 ...

[XmlRoot(Namespace = "http://www.website.com/MyRecord", ElementName="MyRecord")]

给我...

<?xml version="1.0" encoding="utf-8"?>
<q1:MylRecord ID="9266" xmlns:q1="http://www.website.com/MyRecord">
    <q1:List>
        <q1:SpecificItem>

我需要 XML 具有不带前缀的命名空间,因为它将发送给第三方提供商,并且他们拒绝所有其他替代方案。

I am trying to generate an XML document that contains the default namespace without a prefix using XmlSerializer, e.g.

<?xml version="1.0" encoding="utf-8" ?>
<MyRecord ID="9266" xmlns="http://www.website.com/MyRecord">
    <List>
        <SpecificItem>

Using the following code ...

string xmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, string.Empty);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, myRecord, xmlnsEmpty);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlizedString = this.UTF8ByteArrayToString(memoryStream.ToArray());

and class structure ...

[Serializable]
[XmlRoot("MyRecord")]
public class ExportMyRecord
{
    [XmlAttribute("ID")]
    public int ID { get; set; }

Now, I've tried various options ...

XmlSerializer xs = new XmlSerializer
                     (typeof(ExportMyRecord),"http://www.website.com/MyRecord");

or ...

[XmlRoot(Namespace = "http://www.website.com/MyRecord", ElementName="MyRecord")]

gives me ...

<?xml version="1.0" encoding="utf-8"?>
<q1:MylRecord ID="9266" xmlns:q1="http://www.website.com/MyRecord">
    <q1:List>
        <q1:SpecificItem>

I need the XML to have the namespace without the prefix as it's going to a third party provider and they reject all other alternatives.

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

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

发布评论

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

评论(2

昵称有卵用 2024-09-01 19:49:07

就这样:

ExportMyRecord instance = GetInstanceToSerializeFromSomewhere();
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, "http://www.website.com/MyRecord");
var serializer = new XmlSerializer(
    instance.GetType(), 
    "http://www.website.com/MyRecord"
);

There you go:

ExportMyRecord instance = GetInstanceToSerializeFromSomewhere();
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, "http://www.website.com/MyRecord");
var serializer = new XmlSerializer(
    instance.GetType(), 
    "http://www.website.com/MyRecord"
);
倥絔 2024-09-01 19:49:07

这是可用于任何类型的通用实现:

public static void Serialize<T>(T instance, string defaultNamespace, Stream stream)
{
    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty, defaultNamespace);
    var serializer = new XmlSerializer(typeof(T), defaultNamespace);
    serializer.Serialize(stream, instance, namespaces);
}

Here's a generic implementation that can be used for any type:

public static void Serialize<T>(T instance, string defaultNamespace, Stream stream)
{
    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty, defaultNamespace);
    var serializer = new XmlSerializer(typeof(T), defaultNamespace);
    serializer.Serialize(stream, instance, namespaces);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文