从xml中删除q1和所有名称空间 [英] Remove q1 and all namespaces from xml

查看:155
本文介绍了从xml中删除q1和所有名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了xsd生成的C#POCO对象,我需要将其转换为xml.但是,预期的有效负载与我给定的xsds不匹配.具体来说,我需要省略声明,并从xml对象中删除所有名称空间,以便所涉及的公司接受API请求.

I'm given a xsd generated C# POCO object that I need to convert to xml. The expected payload however doesn't match the xsds I was given. Specifically, I need to omit the declaration and remove all namespaces from the xml object so that the company in question accepts the API request.

问题

给定一个类型T的对象,我想序列化它而无需声明和命名空间.

Given an object of type T, I want to serialize it without declaration and namespace.

我已经摆脱了大部分问题,但是出于某些原因将q1添加到每个元素中.我该如何删除?

I've gotten rid of most of it but q1 has been added to each element for some reason. How do I remove that?

尝试

经过研究,我看到一些帖子提供了一个解决方案,该解决方案创建一个空的xml序列化器名称空间,并使用该对象调用序列化器.那只让我半途而废.

After some research, I saw several posts provide a solution that creates an empty xml serializer namespace and calls serializer with that object. That only got me half way there.

用法

        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        var body = payload.SerializeObject(false, true, ns);

扩展方法

    public static string SerializeObject<T>(this T obj, bool indented, bool omitDeclaration, XmlSerializerNamespaces ns)
    {
        var utf8NoBom = new UTF8Encoding(false);
        var settings = new XmlWriterSettings
        {
            OmitXmlDeclaration = omitDeclaration,
            Indent = indented,
            Encoding = utf8NoBom
        };
        using (MemoryStream ms = new MemoryStream())
        {
            using (var xmlWriter = XmlWriter.Create(ms, settings))
            {
                XmlSerializer xmlSer = new XmlSerializer(typeof(T));
                xmlSer.Serialize(xmlWriter, obj, ns);
                byte[] bytes = ms.ToArray();
                return utf8NoBom.GetString(bytes);
            }
        }
    }

不幸的是,结果看起来像这样.

Unfortunately the results looks like this.

<q1:InventoryFeed xmlns:q1=\"http://thecompany.com/\">
    <q1:InventoryHeader>
        <q1:version>1.4</q1:version>
    </q1:InventoryHeader>
    <q1:inventory>
        <q1:sku>WMSkuCap0180</q1:sku>
        <q1:quantity>
            <q1:unit>EACH</q1:unit>
            <q1:amount>3</q1:amount>
        </q1:quantity>
        <q1:fulfillmentLagTime>1</q1:fulfillmentLagTime>
    </q1:inventory>
</q1:InventoryFeed>

如何完全删除名称空间?

How do I remove the namespace completely?

推荐答案

最简单的方法是对XML进行后处理":

The simplest way is to 'post-process' the XML:

var doc = XDocument.Parse(xml);

doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();

foreach (var element in doc.Descendants())
{
    element.Name = element.Name.LocalName;
}

var xmlWithoutNamespaces = doc.ToString();

另一种选择(因为您不能修改源类的XML属性)是为XmlWriter实现一个装饰器,该装饰器会忽略所有名称空间,但这是一个很大的类,因此会有很多样板委托.

The other option (as you can't amend the source class XML attributes) is to implement a decorator for XmlWriter that ignores all namespaces, but it's quite a large class so there'd be a lot of boilerplate delegation.

这篇关于从xml中删除q1和所有名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆