XML 序列化和命名空间前缀

发布于 2024-08-23 05:16:23 字数 494 浏览 6 评论 0原文

我正在寻找一种使用 C# 的方法,可以将类序列化为 XML 并添加命名空间,但定义该命名空间将使用的前缀。

最终,我尝试生成以下 XML:

<myNamespace:Node xmlns:myNamespace="...">
  <childNode>something in here</childNode>
</myNamespace:Node>

我知道使用 DataContractSerializerXmlSerializer 我可以添加一个命名空间,但它们似乎在内部生成一个前缀,我无法控制的事情。我可以使用这些序列化器中的任何一个来控制它(我可以使用它们中的任何一个)吗?

如果我无法控制命名空间的生成,我是否需要编写自己的 XML 序列化程序,如果是的话,最好的编写方法是什么?

I'm looking for a way with C# which I can serialize a class into XML and add a namespace, but define the prefix which that namespace will use.

Ultimately I'm trying to generate the following XML:

<myNamespace:Node xmlns:myNamespace="...">
  <childNode>something in here</childNode>
</myNamespace:Node>

I know with both the DataContractSerializer and the XmlSerializer I can add a namespace, but they seem to generate a prefix internally, with something that I'm not able to control. Am I able to control it with either of these serializers (I can use either of them)?

If I'm not able to control the generation of the namespaces will I need to write my own XML serializer, and if so, what's the best one to write it for?

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

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

发布评论

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

评论(3

祁梦 2024-08-30 05:16:23

要控制命名空间别名,请使用XmlSerializerNamespaces

[XmlRoot("Node", Namespace="http://flibble")]
public class MyType {
    [XmlElement("childNode")]
    public string Value { get; set; }
}

static class Program
{
    static void Main()
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("myNamespace", "http://flibble");
        XmlSerializer xser = new XmlSerializer(typeof(MyType));
        xser.Serialize(Console.Out, new MyType(), ns);
    }
}

如果您需要在运行时更改命名空间,您还可以使用XmlAttributeOverrides

To control the namespace alias, use XmlSerializerNamespaces.

[XmlRoot("Node", Namespace="http://flibble")]
public class MyType {
    [XmlElement("childNode")]
    public string Value { get; set; }
}

static class Program
{
    static void Main()
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("myNamespace", "http://flibble");
        XmlSerializer xser = new XmlSerializer(typeof(MyType));
        xser.Serialize(Console.Out, new MyType(), ns);
    }
}

If you need to change the namespace at runtime, you can additionally use XmlAttributeOverrides.

一向肩并 2024-08-30 05:16:23

当使用从类型具有命名空间的模式生成的代码时,此命名空间覆盖适用于根级别,但不同类型内的标签将具有与该类关联的命名空间。

我有一次需要使用两个不同的生成类,但根据我正在交谈的服务器有不同的名称空间(不要问不在我的控制之下)。

我尝试了这里提供的所有替代方法,最后放弃并使用了一种实际上效果很好的蛮力方法。我所做的就是序列化为字符串。然后使用 string.replace 更改命名空间,然后使用字符串编写器从字符串发布流。响应相同 - 捕获字符串 - 操作名称空间,然后从字符串编写器反序列化字符串。

它可能不优雅或使用所有花哨的覆盖,但它完成了工作。

When using generated code from a schema where the types have namespaces this namespace override applies at the root level but the tags within of varying types will have the namespace associated with the class.

I had an occasion to need to use two different generated classes but have different name spaces based on which server I was talking to (don't ask not under my control).

I tried all the overrides offered here and finally gave up and used a kind of brute force method that actually worked pretty well. What I did was serialize to a string. Then use string.replace to change the namespaces then posted the stream from the string by using a stringwriter. Same on the response - capture to a string - manipulate the namespace then deserialize the string from a string writer.

It may not be elegant or use all the fancy overrides but it got the job done.

千寻… 2024-08-30 05:16:23

我从 另一个线程到达这里,该线程看起来“重复”,但实际上并非如此在我看来。

我在那里回答了,但不太清晰,所以让我在这里再次回答。

如果您的目标是这样做,例如:

<sample xmlns:sample="urn:sample:ns"> 
    <something>value<something>
    <sample:somethingelse>value2</sample:somethingelse>
    <new_root xmlns:newns="url:new:ns">
        <newns:item1>sample1</newns:item1> 
    </new_root>
</sample>

我确实为您找到了解决方案:)。

您需要做的是在“new_root”类中添加这样的命名空间声明。

[XmlNamespaceDeclarations] 
public XmlSerializerNamespaces newns;

并在您的 item1 类中添加以下装饰以使用您的名称空间

[XmlElement("item1", Namespace = "url:new:ns")]

I arrived here from another thread that appears "duplicated" but is not in my opinion.

I answered there but is not really legible so let me answer again here.

if your target is to do so something like:

<sample xmlns:sample="urn:sample:ns"> 
    <something>value<something>
    <sample:somethingelse>value2</sample:somethingelse>
    <new_root xmlns:newns="url:new:ns">
        <newns:item1>sample1</newns:item1> 
    </new_root>
</sample>

I did found a solution for you :).

What you need to do is in the "new_root" class add namespace declaration like this.

[XmlNamespaceDeclarations] 
public XmlSerializerNamespaces newns;

and in your item1 classe add the following decoration in order to use your namespace

[XmlElement("item1", Namespace = "url:new:ns")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文