使用XmlSerializer的多个名称空间(Multiple namespaces with XmlSerializer)
这是场景:
我有嵌套类,需要序列化然后在一个XML文档
[XmlRoot(Namespace="http://www.foo.bar/myschema")] public class root { [XmlAttribute] public string version { get; set; } [XmlElement] public child child { get; set; } ... } [XmlRoot(Namespace="http://www.foo.bar/myschema")] public class child { [XmlElement] public int elemA { get; set; } [XmlElement] public string elemB { get; set; } ... }
我在另一个示例中创建了一个方法来删除其他名称空间并设置一个自定义名称空间:
public static void Save<T>(this T type, string path) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type.GetType()); System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces(); ns.Add("", "http://www.foo.bar/myschema"); using(XmlWriter file = XmlWriter.Create(path)) { xs.Serialize(file, type, ns); } }
我得到这个代码作为结果:
<?xml version="1.0" encoding="utf-8"?> <root xmlns="http://www.foo.bar/myschema" version="2.00"> <child> <elemA>1</elemA> ... </child> </root>
但预计这一点:
<?xml version="1.0" encoding="utf-8"?> <root xmlns="http://www.foo.bar/myschema" version="2.00"> <child xmlns="http://www.foo.bar/myschema"> <elemA>1</elemA> ... </child> </root>
我们必须在两个标签中设置自定义名称空间声明。 这可能吗?
编辑:
这是一个真实世界的例子:
<?xml version="1.0" encoding="UTF-8"?> <enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.01"> <idLote>200602220000001</idLote> <NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe31060243816719000108550000000010001234567890" versao="1.01"> ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </NFe> <NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe31060243816719000108550000000010011234567900" versao="1.01"> ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </NFe> <NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe31060243816719000108550000000010021234567916" versao="1.01"> ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </NFe> </enviNFe>
This is the scenario:
I have nested classes and need to serialize then in an xml document
[XmlRoot(Namespace="http://www.foo.bar/myschema")] public class root { [XmlAttribute] public string version { get; set; } [XmlElement] public child child { get; set; } ... } [XmlRoot(Namespace="http://www.foo.bar/myschema")] public class child { [XmlElement] public int elemA { get; set; } [XmlElement] public string elemB { get; set; } ... }
I have created an method based in another example to remove additional namespaces and set a custom one:
public static void Save<T>(this T type, string path) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type.GetType()); System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces(); ns.Add("", "http://www.foo.bar/myschema"); using(XmlWriter file = XmlWriter.Create(path)) { xs.Serialize(file, type, ns); } }
And I get this code as result:
<?xml version="1.0" encoding="utf-8"?> <root xmlns="http://www.foo.bar/myschema" version="2.00"> <child> <elemA>1</elemA> ... </child> </root>
But expected this:
<?xml version="1.0" encoding="utf-8"?> <root xmlns="http://www.foo.bar/myschema" version="2.00"> <child xmlns="http://www.foo.bar/myschema"> <elemA>1</elemA> ... </child> </root>
We must set the custom namespace declaration in both tags. Is this possible?
Edit:
Here is a real-world example:
<?xml version="1.0" encoding="UTF-8"?> <enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.01"> <idLote>200602220000001</idLote> <NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe31060243816719000108550000000010001234567890" versao="1.01"> ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </NFe> <NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe31060243816719000108550000000010011234567900" versao="1.01"> ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </NFe> <NFe xmlns="http://www.portalfiscal.inf.br/nfe"> <infNFe Id="NFe31060243816719000108550000000010021234567916" versao="1.01"> ... <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </NFe> </enviNFe>
原文:https://stackoverflow.com/questions/8232264
最满意答案
本文详细介绍了Windows 8应用程序中的内存管理:
This article describes in detail the memory management in Windows 8 apps:
相关问答
更多-
在没有Visual Studio的情况下创建Windows 8 Metro应用程序(HTML5)(Creating Windows 8 Metro app (HTML5) without Visual Studio)[2021-11-12]
可以在http://social.msdn.microsoft.com/Forums/en-US/toolsforwinapps/thread/5013262a找到可用于打包和部署应用程序然后从“开始”屏幕运行的PowerShell脚本信息。 -2a18-4ce6-99d1-03451d6c9619 。 问题在于,这不会让你有很大的调试空间,这是像VS这样的工具的最大价值之一。 (并且不要忘记Blend ...一个用于样式和调试CSS的优秀工具)。 我听说人们确实被VS(包括我自己)吓到了,但Express ... -
答案是在这篇文章中: “提琴手和Windows 8 Metro风格的应用程序” 。 结果表明“连接到Fiddler需要应用程序功能或环回豁免”。 The answer is in this article: "Fiddler and Windows 8 Metro-style applications". Turns out that "connecting to Fiddler requires an Application Capability or Loopback Exemption".
-
您应该能够将进程打包为进程外COM对象,并使用CoCreateInstanceFromApp启动它。 可能存在一些与使COM接口的代理/存根工作相关的挑战 - 目前没有很好地记录低级C ++ / IDL创作经验,因此构建代理/存根将很困难,但绝对可能去做。 You should be able to package your process as an out-of-proc COM object and use CoCreateInstanceFromApp to launch it. There are ...
-
您只能使用C#,VB,JavaScript和C ++创建地铁样式应用程序。 目前不支持使用Java编写地铁样式应用程序。 You can only create metro style apps with C#, VB, JavaScript and C++. There is currently no support for authoring metro style applications with Java.
-
本文详细介绍了Windows 8应用程序中的内存管理: http://msdn.microsoft.com/en-us/magazine/jj651575.aspx This article describes in detail the memory management in Windows 8 apps: http://msdn.microsoft.com/en-us/magazine/jj651575.aspx
-
如何从metro风格的JavaScript应用程序启动Windows服务?(How to start a windows service from a metro style javascript app?)[2022-12-27]
AFAIK,这是不可能的。 现代应用程序与桌面环境之间没有直接通信。 AFAIK, it is not possible. There is no direct communication between modern apps and the desktop environment. -
您只能在Web上下文中运行的页面中包含外部js文件。 有关这方面的更多信息,请访问: http://msdn.microsoft.com/en-us/library/windows/apps/hh465373.aspx You can only include external js files from pages running in the web context. More about this you can find here: http://msdn.microsoft.com/en-us/l ...
-
是的,有一个很棒的名为MetroJS的处理瓷砖动画。 但是由您来管理页面上的显示。 http://www.drewgreenwell.com/projects/metrojs 效果很好! 我使用此应用程序来创建Windows应用商店中的应用Tweaker,查看社交类别中的免费试用版。 所以它在Windows 8上也很棒。 我对WinJS上没有VariableSizedWrapGrid控件感到有点失望。这是您想要的控件,处理多个大小的磁贴并将它们整齐地包装在一起,遗憾的是目前仅在XAML上可用。 http:/ ...
-
如何在Windows Metro Style App上重新加载iframe的内容(How can I reload the contents of iframe on Windows Metro Style App)[2023-05-04]
这是因为相同的原产地政策 。 你应该写一些类似的东西: iframe.contentWindow.location.href = "http://www.example.com"` 要更新框架内页面的URL,例如,可以将其postMessage到框架的父级 - 如果您有权访问页面的源代码 It's because of the same origin policy. You should write something like: iframe.contentWindow.location.href = ... -
无论操作系统如何,Mah Apps都会在WPF默认样式之上提供自定义样式。 你说你想在你的WPF应用程序中使用Metro Style。 所以你仍然应该在Windows 8.1中使用MahApps。 据我所知,Windows 8.1桌面模式与MahApps看起来不像Metro Style。 Mah Apps provide a custom style on top of your WPF default style regardless of operating system. You said you w ...