C#学习教程:在嵌套属性上使用XmlAttributeOverrides它适用于“根”类的属性,但不适用于嵌套属性。这是一个简单的例子来说明我想要完成的事情。我的类层次结构如下:publicclassMain{publicstringName{get;放;}publicLocationAddress{get;放;}}publicclassLocation{publicstringStreetAddress{get;放;}publicContactContactInfo{get;放;}}publicclassContact{publicstringPhoneNumber{get;放;}publicstringEmailAddr{get;放;当我序列化Main()时,我得到这样的结果:我能做的是使用Keepnamesoraddressespresent:XmlAttributes属性=newXmlAttributes();attribs.XmlIgnore=true;attribs.XmlElements.Add(newXmlElementAttribute("地址"));overrides.Add(typeof(Main),"Address",attribs);xs=newXmlSerializer(typeof(Main),overrides);我还需要做的是保持Main.Address.ContactInfo序列化SOMETIMES(如果它是空的)。我尝试了以下但它们不起作用:XmlAttributeOverridesoverrides=newXmlAttributeOverrides();XmlAttributes属性=newXmlAttributes();attribs.XmlIgnore=true;attribs.XmlElements.Add(newXmlElementAttribute("ContactInfo"));overrides.Add(typeof(Contact),"ContactInfo",attribs);xs=newXmlSerializer(typeof(Contact),overrides);和...XmlAttributeOverridesoverrides=newXmlAttributeOverrides();XmlAttributes属性=newXmlAttributes();attribs.XmlIgnore=true;attribs.XmlElements.Add(newXmlElementAttribute("ContactInfo"));overrides.Add(typeof(Main.Address.ContactInfo),"ContactInfo",attribs);xs=newXmlSerializer(typeof(Main.Address.ContactInfo),overrides);我实际上已经尝试了很多,包括使用XPath语句来指定要定位的属性名称,但如果我的尝试失败,我不想填充此页面。我什至可以用这种方法问这个问题吗?有更简单的方法来实现您正在寻找的东西。你说你想要实现的是不序列化/Main/Address/ContactInfo如果ContactInfo包含任何数据。如果您保留代码,它将序列化Main的所有属性,无论它们是null还是空的。第一步,您需要将XmlSerializerNamespaces属性添加到所有对象,否则每个空对象都将被序列化为.这可以很容易地完成,如下所示:publicMyXmlElement{publicMyXmlElement(){//将您自己的默认名称空间添加到您的类型以防止生成xsi:*和xsd:*//属性。this._namespaces=newXmlSerializerNamespaces(newXmlQualifiedName[]{newXmlQualifiedName(string.Empty,"urn:myDefaultNamespace")});}[XmlElement("MyNullableProperty",IsNullable=false)]publicstringMyNullableProperty{get{returnstring.IsNullOrWhiteSpace(this._myNullableProperty)?空:this._myNullableProperty;}设置{this._myNullableProperty=value;}}[XmlNamespacesDeclaration]publicXmlSerializerNamespacesNamespaces{get{returnthis._namespaces;}}私人XmlSerializerNamespaces_namespaces;XML对象的所有相关命名空间。您应该为所有对象提供一个默认名称空间(以上面的代码为模型)。这可以防止在序列化对象时输出xsi:*和xsd:*属性。此外,使用System.Xml.Serialization.XmlElementAttribute指定元素不能为空。此外,通过检查string.IsNullOrWhiteSpace(someVariable)并返回null,则在执行上述操作后该属性将不会被序列化。所以,将这一切放一起为您的位置类:publicclassLocation{//您应该在所有类型上都有一个公共默认构造函数以进行(反)序列化。publicLocation(){this._namespaces=newXmlSerializerNamespaces(newXmlQualifiedName[]{newXmlQualifiedName(string.Empty,"urn:myNamespace");//默认命名空间——防止生成xsi:nil="true",因为以及xsd:*属性。});}publicstringStreetAddress{//如果你不想被生成,这样做:get{returnstring.IsNullOrEmpty(this._streetAddress)?空:this._streetAddress;}//否则,如果你不在乎,就这样做//get;//如果不想生成xsi:nil="true",只需要实现setter。设置{this._streetAddress=value;}//否则,只是//设置;}私有字符串_streetAddress;[XmlElement("ContactInfo",IsNullable=false)]publicContactContactInfo{//你一定要这样做以防止ContactInfo元素//在它为null时输出(即cont没有数据)get{if(this._contactInfo!=null&&string.IsNullOrWhiteSpace(this._contactInfo.PhoneNumber)&&string.IsNullOrWhiteSpace(this._contactInfo.EmailAddr))returnnull;返回这个._contactInfo;}设置{这个。_contactInfo=值;}}私人联系_contactInfo;[XmlNamespacesDeclarations]publicXmlSerializerNamespacesNamespaces{get{returnthis._namespaces;}}私有XmlSerializerNamespaces空间_namespaces;当ContactInfo本身为null时,不应再将空的ContactInfo属性序列化为XML您应该对其他对象进行类似的更改。有关.NETXML序列化的更多信息,请参阅我的其他stackoverflow回答:对于任何其他尝试使用XmlAttributeOverrides执行此操作的人,结果@user1437872非常接近找到答案。下面是覆盖嵌套元素ContactInfo的覆盖代码。以上就是C#学习教程:在嵌套属性上使用XmlAttributeOverrides,分享全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注—XmlAttributeOverridesoverrides=newXmlAttributeOverrides();XmlAttributes属性=newXmlAttributes();attribs.XmlIgnore=true;attribs.XmlElements.Add(newXmlElementAttribute("ContactInfo"));overrides.Add(typeof(Address),"ContactInfo",attribs);xs=newXmlSerializer(typeof(Main),overrides);本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
