English 中文(简体)
具有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>

我们必须在两端确定习惯名称。 这是可能的吗?

Edit:

这里是一个真正的世界例子:

<?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>
最佳回答

从XML角度来看,你的例子完全相同,因此第一个例子完全是罚款。 如果你必须使用第二种方法,那么,我们理解的XML或处理管道是一个严重问题。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签