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
How do I ignore an [XMLIgnore] Attribute
提问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 Definition
for 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.XmlSerializer
to 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 XMLIgnore
attributes but it'd be nice if XmlSerializer
had 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))
((见第一注))