Remove q1 and all namespaces from xml

前端 未结 3 1005
渐次进展
渐次进展 2021-01-18 22:14

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

3条回答
  •  猫巷女王i
    2021-01-18 22:34

    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();
    

    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.

提交回复
热议问题