简体   繁体   English

XML 序列化和命名空间前缀

[英]XML Serialization and namespace prefixes

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

Ultimately I'm trying to generate the following XML:最终我试图生成以下 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.我知道使用DataContractSerializerXmlSerializer我可以添加一个命名空间,但它们似乎在内部生成了一个前缀,我无法控制。 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?如果我无法控制命名空间的生成,我是否需要编写自己的 XML 序列化程序,如果是这样,编写它的最佳目的是什么?

To control the namespace alias, use XmlSerializerNamespaces .要控制命名空间别名,请使用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 .如果您需要在运行时更改命名空间,您还可以使用XmlAttributeOverrides

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.然后使用 string.replace 更改命名空间,然后使用 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.它可能不优雅或使用所有花哨的覆盖,但它完成了工作。

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.您需要做的是在“new_root”类中添加这样的命名空间声明。

[XmlNamespaceDeclarations] 
public XmlSerializerNamespaces newns;

and in your item1 classe add the following decoration in order to use your namespace并在您的 item1 类中添加以下装饰以使用您的命名空间

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM