C# 如何忽略 [XMLIgnore] 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1383755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:54:23  来源:igfitidea点击:

How do I ignore an [XMLIgnore] Attribute

c#.netxml.net-3.5xml-serialization

提问by Eoin Campbell

I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to an XML File.

我正在尝试将从 3rd Party .NET Lib 获得的一些对象序列化为 XML 文件。

When I Go To Definitionfor the object, some of the Properties of that object are marked as [XMLIgnore]

当我Go To Definition为对象时,该对象的一些属性被标记为[XMLIgnore]

Is there any way to tell my System.Xml.Serialization.XmlSerializerto ignore the fact that some properties have that attribute and that it should serialize everything in the object.

有什么方法可以告诉我System.Xml.Serialization.XmlSerializer忽略某些属性具有该属性并且它应该序列化对象中的所有内容的事实。

I could probably obtain the source and recompile it without the XMLIgnoreattributes but it'd be nice if XmlSerializerhad some nice override property like

我可能可以获得源代码并在没有XMLIgnore属性的情况下重新编译它,但是如果XmlSerializer有一些不错的覆盖属性就好了

XmlSerializer xmls = new XmlSerializer(
   typeof(MyObject),
   Settings.DoNotApplyXMLAttributeRules
);

Thanks in advance

提前致谢



EDIT

编辑

Have tried the XmlAttributeOverrides as suggested but not having much joy. Here's the object definition (it's from the FlickrAPI for a Photo)

按照建议尝试了 XmlAttributeOverrides,但没有太多乐趣。这是对象定义(来自 FlickrAPI for a Photo)

[Serializable]
public class Photo
{
    //Some code omitted
    [XmlIgnore]
    public string LargeUrl { get; }

}

And heres the serializer code I've written... still doesn't work...

这是我编写的序列化程序代码......仍然无法正常工作......

XmlWriter xtw = XmlWriter.Create( Server.MapPath("~/App_Data/Data.xml") );

XmlAttributes photoAttributes = new XmlAttributes();
photoAttributes.XmlIgnore = false;

XmlAttributeOverrides photoOverrides = new XmlAttributeOverrides();
photoOverrides.Add(typeof(Photo), "LargeUrl", photoAttributes);

XmlSerializer xmlphoto = new XmlSerializer(typeof(Photo), photoOverrides);

采纳答案by manji

use:

用:

XmlAttributeOverrides

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx

Edit: (Following the question EDIT)

编辑:(在问题编辑之后)

the property must be public and have a getter and setter to be serialized.

该属性必须是公共的,并且具有要序列化的 getter 和 setter。

http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx

((see first Note))

((见第一注))