【发布时间】:2015-02-27 18:10:48
【问题描述】:
我正在尝试使用 XMLSerializerFormat 和 WCF 设置我的命名空间。但我似乎无法让命名空间出现在对象的根目录上。如果我使用基本的 xmlserializer 看起来就好了,但通过网络传输时就不行了。
这是使用基本 xmlserializer 时的样子
<?xml version="1.0" encoding="utf-16"?> <bar:SomeObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:bar="http://www.bar.com/"> <bar:Name>FooBar</bar:Name> <Foo:SomeObject xmlns:Foo="http://www.Foo.com/">
<Foo:FirstName>Foo</Foo:FirstName>
<Foo:LastName>Bar</Foo:LastName> </Foo:SomeObject> <Foo:SomeObject xmlns:Foo="http://www.Foo.com/">
<Foo:FirstName>Bar</Foo:FirstName>
<Foo:LastName>Foo</Foo:LastName> </Foo:SomeObject> </bar:SomeObjects>
请注意,SomeObjects 根的命名空间为 bar。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="e16015ee-718d-4e2a-845b-0214e4caa708" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">9bcdae91-074d-4802-b4f3-c11f310d7b13</ActivityId>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GiveMeSomeObjects xmlns="http://tempuri.org/">
<SomeObjects xmlns:bar="http://www.bar.com/">
<bar:Name>FooBar</bar:Name>
<foo:SomeObject xmlns:foo="http://www.foo.com/">
<FirstName xmlns="http://www.Foo.com/">Foo</FirstName>
<LastName xmlns="http://www.Foo.com/">Bar</LastName>
</foo:SomeObject>
<foo:SomeObject xmlns:foo="http://www.foo.com/">
<FirstName xmlns="http://www.Foo.com/">Bar</FirstName>
<LastName xmlns="http://www.Foo.com/">Foo</LastName>
</foo:SomeObject>
</SomeObjects>
</GiveMeSomeObjects>
</s:Body>
</s:Envelope>
请注意,根“SomeObjects”没有命名空间前缀。
在我的代码中,我创建了以下代码,以向 wsdl 创建的代理类的部分类添加构造函数。
[XmlRootAttribute("SomeObjects", Namespace = "http://www.bar.com/")]
public partial class SomeObjects
{
public SomeObjects()
{
xmlsnField = new XmlSerializerNamespaces();
this.xmlsnField.Add("bar", "http://www.bar.com/");
this.xmlsnField.Add("foo", "http://www.foo.com/");
}
}
【问题讨论】:
标签: c# xml wcf namespaces