简体   繁体   中英

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

<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; 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 . Ensure that you are using the proper namespace everywhere.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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