本文介绍了带有null元素的Xmlserialization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个基于我生成架构类的xml架构。 我使用的是xmlserializer,我试图添加I have a xml schema that based on that I have generated the schema class.I am using xmlserializer and I have tried to addnillable="true"以便它显示非空的元素。 我尝试过: 它向我显示如下结果: so that it shows me the elements that are not null.What I have tried:it shows me the result like this:<Info> <Fl> <Name>Test</Name> <News d3p1:nil="true" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"/> </Fl></Info> 但我希望得到如下结果:but I want the result like this:<Info> <Fl> <Name>Test</Name> <News/> </Fl></Info> 这是我的C#代码:This is my C# code :Info resp = new Info();resp.Fl = data;var serializer = new XmlSerializer(typeof(Info));using (var stream = new StreamWriter("E:\\report.xml")){XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();namespaces.Add(string.Empty, string.Empty);serializer.Serialize(stream, resp, namespaces);bRet = true;} 这是我的xsd:and this my xsd :<xs:schema id="Response" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified"> <xs:element name="Info"> <xs:complexType> <xs:sequence> <xs:element name="Fl" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element type="xs:string" name="Name" nillable="true"/> <xs:element type="xs:string" name="News" nillable="true"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema> 我怎样才能得到我的结果期待?How can I have the result I am expecting?推荐答案nillable="true" 尝试tryminOccurs="0" 这将省去元素所有在一起。This will leave out the element all together.<Info> <Fl> <Name>Test</Name> </Fl></Info><xs:element default="" name="News" type="xs:string" /> 或 您可以将这些类变量初始化为空字符串 问候 Praveenoryou can initialise those class variables to empty stringRegardsPraveen 这篇关于带有null元素的Xmlserialization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!