C# XmlSerializer在序列化时向所有元素添加名称空间

C# XmlSerializer在序列化时向所有元素添加名称空间,c#,xml,serialization,xml-namespaces,xmlserializer,C#,Xml,Serialization,Xml Namespaces,Xmlserializer,我有一个序列化函数,用于序列化实体(客户对象)。XmlSerializer是使用 new XmlSerializer(entity.GetType()) 我有两个测试用例调用这个函数。第一个测试获取一个客户列表,并发送一个活动客户进行序列化,这很好。创建的XML输出仅在根元素(即customer元素/标记)中具有名称空间 例如: <Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="htt

我有一个序列化函数,用于序列化实体(客户对象)。XmlSerializer是使用

          new XmlSerializer(entity.GetType())
我有两个测试用例调用这个函数。第一个测试获取一个客户列表,并发送一个活动客户进行序列化,这很好。创建的XML输出仅在根元素(即customer元素/标记)中具有名称空间

例如:

<Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" domain="XXX" 
sparse="false" xmlns="http://schema.xxx.com/finance/v3">
   <Id>1099</Id>...
<Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" domain="XXX" 
sparse="false">
    <Id xmlns="http://schema.xxx.com/finance/v3">1099</Id>...

1099...
第二个测试调用Customer queryservice并使用LinqExtender从query service查询结果,并返回一个活动客户。当第二个测试调用serialize方法时,它会使XML输出将名称空间添加到所有元素中,而不是仅为根元素添加名称空间

例如:

<Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" domain="XXX" 
sparse="false" xmlns="http://schema.xxx.com/finance/v3">
   <Id>1099</Id>...
<Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" domain="XXX" 
sparse="false">
    <Id xmlns="http://schema.xxx.com/finance/v3">1099</Id>...

1099...
我比较了第一次和第二次测试使用的两个对象,它们是相同的


我希望序列化程序在这种情况下保持一致,即只在根元素中创建名称空间的XML输出。

第一种情况和第二种情况下,什么是
entity.GetType()
?在这两种情况下,它都解析为客户类型:-(你为什么在意?它们在语义上是相同的。@JohnSaunders-不,它们不是。在第一种情况下,
Customer
及其子项属于
”http://schema.intuit.com/finance/v3“
;在第二个
中,Customer
没有名称空间,其子项属于
”http://schema.xxx.com/finance/v3“
。(除非问题中有输入错误。)@dbc:我希望在某个地方会发现输入错误。如果它是相同的
Customer
类,那么在这两种情况下它应该是相同的XML名称空间。