与XmlSerializer的多个命名空间 [英] Multiple namespaces with XmlSerializer
问题描述
这是该方案:
我有嵌套类,需要在一个XML文档然后串行化
[XmlRoot(命名空间=http://www.foo.bar/myschema)
公共类根
{
[XmlAttribute]
公共字符串版本{获得;组; }
[的XmlElement]
公共孩童{获得;组; }
...
}
[XmlRoot(命名空间=http://www.foo.bar/myschema)
公共类子
{
[的XmlElement]
公众诠释elemA {获得;组; }
[的XmlElement]
公共字符串elemB {获得;组; }
...
}
我已经创建了一个基于在另一个例子的方法来消除额外的命名空间,并设置一个自定义的:
公共静态无效的保存< T>(这件T型,字符串路径)
{
System.Xml.Serialization.XmlSerializer XS =
新System.Xml.Serialization.XmlSerializer(type.GetType());
System.Xml.Serialization.XmlSerializerNamespaces NS =
新System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add(,http://www.foo.bar/myschema);
使用(XmlWriter的文件= XmlWriter.Create(路径))
{
xs.Serialize(文件类型,NS);
}
}
和我得到这个code的结果是:
< XML版本=1.0编码=UTF-8&GT?;
<根的xmlns =http://www.foo.bar/myschema版本=2.00>
<孩子>
&其中; elemA→1&其中; / elemA>
...
< /儿童>
< /根>
但预计这样的:
< XML版本=1.0编码=UTF-8&GT?;
<根的xmlns =http://www.foo.bar/myschema版本=2.00>
<孩子的xmlns =http://www.foo.bar/myschema>
&其中; elemA→1&其中; / elemA>
...
< /儿童>
< /根>
我们必须设定自定义命名空间声明在这两个标签。这可能吗?
编辑:
下面是一个真实的例子:
< XML版本=1.0编码=UTF-8&GT?;
< enviNFe的xmlns =http://www.portalfiscal.inf.br/nfeversao =1.01>
< idLote> 200602220000001< / idLote>
<非正规教育的xmlns =http://www.portalfiscal.inf.br/nfe>
&所述; infNFe标识=NFe31060243816719000108550000000010001234567890versao =1.01>
...
<签名的xmlns =http://www.w3.org/2000/09/xmldsig#>
...
< / NFE>
<非正规教育的xmlns =http://www.portalfiscal.inf.br/nfe>
&所述; infNFe标识=NFe31060243816719000108550000000010011234567900versao =1.01>
...
<签名的xmlns =http://www.w3.org/2000/09/xmldsig#>
...
< / NFE>
<非正规教育的xmlns =http://www.portalfiscal.inf.br/nfe>
&所述; infNFe标识=NFe31060243816719000108550000000010021234567916versao =1.01>
...
<签名的xmlns =http://www.w3.org/2000/09/xmldsig#>
...
< / NFE>
< / enviNFe>
从一个XML来看你的例子是相同的,所以第一个是完全正常的。如果你必须使用第二个,有一个与我们的XML的理解或处理管道的一个严重问题。
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>
From an XML point of view your examples are identical, so the first one is perfectly fine. If you have to use the second one, there is a serious problem with our XML understanding or your processing pipeline.
这篇关于与XmlSerializer的多个命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!