C# XML 序列化和命名空间前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2339782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Serialization and namespace prefixes
提问by Aaron Powell
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. Am I able to control it with either of these serializers (I can use either of them)?
我知道使用 theDataContractSerializer
和 the XmlSerializer
I 都可以添加命名空间,但它们似乎在内部生成了一个前缀,而有些东西我无法控制。我可以使用这些序列化程序中的任何一个来控制它吗(我可以使用它们中的任何一个)?
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 序列化程序,如果是这样,编写它的最佳目的是什么?
采纳答案by Marc Gravell
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 namespaceat runtime, you can additionally use XmlAttributeOverrides
.
如果您需要在运行时更改命名空间,您还可以使用XmlAttributeOverrides
.
回答by impangolin
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.
我尝试了这里提供的所有覆盖,最后放弃并使用了一种实际上效果很好的蛮力方法。我所做的是序列化为一个字符串。然后使用 string.replace 更改命名空间,然后使用 stringwriter 从字符串中发布流。响应相同 - 捕获到字符串 - 操作命名空间,然后从字符串编写器反序列化字符串。
It may not be elegant or use all the fancy overrides but it got the job done.
它可能不优雅或使用所有花哨的覆盖,但它完成了工作。
回答by Guillermo Gutiérrez
I arrived here from another threadthat 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")]