调整对象图的XAML序列化(Tweaking XAML serialization of an object graph)

编程入门 行业动态 更新时间:2024-10-27 19:27:56
调整对象图的XAML序列化(Tweaking XAML serialization of an object graph)

我的应用程序使用内置的.NET解决方案从内存中的对象生成XAML文件: System.XamlServices.Save(Stream, Object)但是,我得到的XAML被严重污染。 所有属性始终是序列化的。 有没有办法告诉XAML序列化程序忽略保存其默认值的属性? (我几乎可以完全控制序列化的类。)

Expression Blend解决了这个问题:如果我们不更改属性,生成的XAML文件将根本不包含它。 生成的XAML并没有包含所有可能的属性及其默认值,并且看起来不像常规自动生成的混乱。 我也想拥有漂亮的XAML。

My application generates XAML files from in-memory objects using the built-in .NET solution: System.XamlServices.Save(Stream, Object) However, the XAML I get is polluted badly. All properties are always serialized. Is there a way that to tell the XAML serializer to ignore properties that hold their default values? (I have almost full control over the classes that are serialized.)

Expression Blend solves this wonderfully: if we do not change a property, the generated XAML file will not contain it, at all. The resulted XAML is not cluttered with all the possible properties and their default values and does not look like the regular autogenerated mess. I would like to have beautiful XAML, too.

最满意答案

仅使用一个对象执行此操作:

一旦:

使用默认属性创建一个新对象 将其序列化为XAML 将其加载到XML DOM中 通过每个子节点和子子节点递归 将每个保存到一个字典中,其中键为节点的XPath,值为innerXML。 显然,如果你有相同路径的节点,这将无法工作,你将不得不限定它们。 但在这种情况下,你可能想要永远不要修剪它们。 你不再需要那个对象了。 推它,保持字典。

对于每个对象:

将该对象序列化为XAML 加载到XML DOM中 通过每个子节点和子子节点递归 在字典中查找每个XPath。 如果innerXML是相同的,请在应该害怕的节点列表中注意这一点。 完成递归后,向这些节点显示他们害怕的原因 - 删除它们! DOM应该有漂亮的XAML。

我让你在字典中使用相同的XPath来使嵌套对象更好地工作。 然而,这取决于你的对象是什么以及它们如何嵌套以便能够与之对话。 祝你好运!

To do this with just one object:

Once:

Make a new object with default properties Serialize it to XAML Load it into an XML DOM Recurse thru each child and sub-child node Save each into a dictionary with the key the XPath of the node, the value the innerXML. Obviously, if you have nodes with the same path this won't work, you'll have to qualify them. But in such case you'd probably want to never prune those anyway. You don't need that object anymore. Pitch it, keep the Dictionary around.

For each object:

Serialize that object to XAML Loadit into an XML DOM Recurse thru each child and sub-child node Look up each XPath in the dictionary. If the innerXML is the same, note this in a list of nodes that should be afraid. When done recursing, show those nodes why they are afraid - DELETE them! The DOM should have beautiful XAML in it.

I had you same the XPath in the dictionary to make nested objects work better. It depends what your objects are and how they are nested for me to be able to speak to that, however. good luck!

更多推荐

本文发布于:2023-07-30 13:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   序列化   XAML   Tweaking   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!