简体   繁体   English

从子元素中删除XML名称空间

[英]Remove XML namespace from child element

I'm not sure quite how to do this but i need to generate an excel file as follows 我不确定如何执行此操作,但是我需要生成一个excel文件,如下所示

<abc: Declarant>
<Type>Test</Type>
<ReferenceNo>TESTREF</ReferenceNo>
<Provider>Me</Provider>
</abc: Declarant>

Note how the namespace is only prefixed on the higher level. 请注意,名称空间是如何仅在更高级别上前缀的。 I've no problem serializing this either with or without the namespace prefix but cant seem to get it without. 无论是否使用名称空间前缀,我都可以将其序列化,但是没有它似乎无法得到它。

Heres what i have before my serialisation 这是我序列化之前的东西

XmlSerializer x = new XmlSerializer(t.GetType());
            System.IO.StreamWriter file = new System.IO.StreamWriter(
            path);
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("abc", "http://www.iwebcms.com");

            x.Serialize(file, t,ns);

and declarant is as follows 声明如下

[XmlElement(ElementName = "Declarant", Namespace = " http://www.iwebcms.com ")] public Declarant declarant { get; [XmlElement(ElementName =“ Declarant”,Namespace =“ http://www.iwebcms.com ”)]公共声明符 set; 组; } }

[Serializable()]
    public class Declarant
    {
        [XmlElement(ElementName = "Type")]
        public string Type { get; set; }

        [XmlElement(ElementName = "ReferenceNo")]
        public string ReferenceNo { get; set; }

        [XmlElement(ElementName = "Provider")]
        public string Provider { get; set; }
}

Any help would be greatly appreciated :( 任何帮助将不胜感激 :(

Thanks 谢谢

You seem to be working with two different Namespaces, in one place you have it defined as http://www.mysite , in the other it is defined as http://www.iwebcms.com . 您似乎正在使用两个不同的命名空间,在一个地方将其定义为http://www.mysite ,在另一个地方将其定义为http://www.iwebcms.com Ensure that you are using the proper namespace everywhere. 确保在各处使用正确的名称空间。

我最终只是将文件作为原始文本循环浏览,并替换了我需要的名称空间-令人讨厌,但让我开始....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM