删除Serializer创建的空xmlns是序列化的。我的问题是数据协定有一些内部对象。序列化程序将一个空的命名空间属性添加到内部对象的开始标记中。有没有办法阻止这种情况发生?如何使内部对象与根目录属于同一命名空间?因此,省略后代的xmlns声明是正确的。您可以使用[assembly:ContractNamespace]属性覆盖程序集中所有协定的命名空间。有关示例,请参见数据协定名称。编辑:下面的一些例子详细说明。假设您手动构建XML文档并且没有为任何元素分配名称空间。XDocumentxmlDocument=newXDocument(newXElement("Book",newXElement("Title","AnimalFarm"),newXElement("Author","GeorgeOrwell"),newXElement("Publisher",newXElement("名称","SeckerandWarburg"),newXElement("Location","London"),newXElement("Founded",1910))));返回xmlDocument.ToString();正如预期的那样,生成的XML将是Nonamespacedeclaration:AnimalFarmGeorgeOrwellSeckerandWarburgLondon1910但是,如果仅使用名称空间指定了根元素,则所有子元素都必须使用xml=""显式地从此默认名称空间恢复。根据命名空间默认规则:默认命名空间声明的范围从它出现的开始标记的开头延伸到相应的结束标记的结尾,不包括任何内部默认命名空间声明的范围。对于空标签,范围是标签本身。因此,以下代码(使用为根元素指定的名称空间)...XDocumentxmlDocument=newXDocument(newXElement("{http://example.com/library}Book",newXElement("Title","动物农场”),newXElement(“作者”,“乔治奥威尔”),newXElement(“出版商”,newXElement(“名称”,“SeckerandWarburg”),newXElement(“位置”,“伦敦”),newXElement("Founded",1910))));返回xmlDocument.ToString();...将提供以下XML:AnimalFarmGeorgeOrwellSeckerandWarburgLondon1910注意元素的子项不需要从根的命名空间中恢复,因为它们从父元素继承了“无命名空间”声明。为了消除xmlns=""声明,为了演示,我们可以为所有后代分配相同的命名空间:XDocumentxmlDocument=newXDocument(newXElement("{http://example.com/library}Book",newXElement("{http://example.com/library}Title","AnimalFarm"),newXElement("{http://example.com/library}Author","GeorgeOrwell"),newXElement("{http://example.com/library}Publisher",newXElement("{http://example.com/library}Name","SeckerandWarburg"),newXElement("{http://example.com/library}Location","London"),newXElement("{http://example.com/library}Founded",1910))));返回xmlDocument.ToString();这将仅在声明于(并在所有后代中隐式继承)的根命名空间中提供XML文档:AnimalFarmGeorgeOrwellSecker和WarburgLondon1910要模拟涉及Web服务的场景,我们可以创建以下WCF服务。[DataContract]publicclassBook{[DataMember]publicstringTitle{get;放;}[DataMember]publicstringAuthor{get;放;}[DataMember]publicPublisherPublisher{get;放;}}[DataContract]publicclassPublisher}[DataMember]publicstringLocation{get;放;}[DataMember]publicshortFounded{get;放;}}[ServiceContract]publicinterfaceILibraryService{[OperationContract]BookGetBook();}publicclassLibraryService:ILibraryService{publicBookGetBook(){returnnewBook{Title="AnimalFarm",Author="GeorgeOrwell",Publisher=newPublisher{Name="SeckerandWarburg",Location="伦敦",创立=1910,}};我们在客户端应用程序中添加对上述服务的服务引用,使用它的操作,并序列化结果,同时将它们包装在具有显式命名空间的根目录中。){varbook=libraryClient.GetBook();varstringBuilder=newStringBuilder();使用(XmlWriterxmlWriter=XmlWriter.Create(stringBuilder)){xmlWriter.WriteStartElement("图书","http://example.com/library");varserializer=newXmlSerializer(book.GetType());serializer.Serialize(xmlWriter,book);xmlWriter.WriteEndElement();}返回stringBuilder.ToString();在这种情况下,内部元素Book包含xmlns=""声明GeorgeOrwell1910LondonSeckerandWarburgAnimalFarmxmlns=""通过将其名称空间(及其后代名称空间)设置为对应于根目录。对于XmlSerializer类,可以通过其构造函数的第二个参数指定所有元素的默认命名空间。(实际技术会因您使用的序列化策略而异。)使用(varlibraryClient=newLibraryServiceReference.LibraryServiceClient()){varbook=libraryClient.GetBook();varstringBuilder=newStringBuilder();使用(XmlWriterxmlWriter=XmlWriter.Create(stringBuilder)){xmlWriter.WriteStartElement("Books","http://example.com/library");varserializer=newXmlSerializer(book.GetType(),"http://example.com/library");serializer.Serialize(xmlWriter,book);xmlWriter.WriteEndElement();}返回stringBuilder.ToString();这将产生预期的结果:GeorgeOrwell1910LondonSeckerandWarburgAnimalFarm如果您可以控制序列化程序,则始终可以添加一个空名称空间以确保从输出XML中省略xmlns。例如:varserializer=newXmlSerializer(target.GetType());varns=newXmlSerializerNamespaces();ns.Add("","");serializer.Serialize(xmlWriter,target,ns);此致,这可能有点偏离主题,如果您使用的是[DataContract],它可能不适用。相反,这适用于从WSDL生成的代理代码(在具有Java端点的互操作环境中,我们被告知xmlns=""无效)。所以我把它放在那里以防它有帮助。当设置为System.Xml.Schema.XmlSchemaForm.Unqualified时,XmlElementAttribute.Form属性会导致WCF请求中的子成员输出xmlns=""。[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]publicstringMyProperty{get;放;它产生了一些东西我的价值在这里将它设置为System.Xml.Schema。XmlSchemaForm.None(默认)意味着它不会输出“不合格”的命名空间属性。[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.None)]publicstringMyProperty{get;放;产生这个:我的价值在这里我不确定你是否可以在导入wsdl引用时改变这种行为,或者wsdl应该已经改变,但我最终直接编辑了生成的代理代码(这绝对不理想),但实现了我的直接目标。以上就是C#学习教程:删除序列化程序创建的空xmlns。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
