Remove q1 and all namespaces from xml

前端 未结 3 967
渐次进展
渐次进展 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-18 22:35

    This is another solution:

    XmlWriterSettings settings = new XmlWriterSettings();
    //If you wish Encoding
    settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
    
    using (XmlWriter xmlWriter = XmlWriter.Create(tempFilePath, settings))
    {
         var ns = new XmlSerializerNamespaces();
         ns.Add("", "http://thecompany.com");
         XmlSerializer s = new XmlSerializer(YOUROBJECT.GetType(), "http://thecompany.com");    
         s.Serialize(xmlWriter, YOUROBJECT, ns);
    
    }
    
    0 讨论(0)
  • 2021-01-18 22:36

    I sometimes use RegEx or XML Linq

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input =
                    "<q1:InventoryFeed xmlns:q1=\"http://thecompany.com/\">\n" +
                        "<q1:InventoryHeader>\n" +
                            "<q1:version>1.4</q1:version>\n" +
                        "</q1:InventoryHeader>\n" +
                        "<q1:inventory>v" +
                            "<q1:sku>WMSkuCap0180</q1:sku>\n" +
                            "<q1:quantity>\n" +
                                "<q1:unit>EACH</q1:unit>\n" +
                                "<q1:amount>3</q1:amount>\n" +
                            "</q1:quantity>\n" +
                            "<q1:fulfillmentLagTime>1</q1:fulfillmentLagTime>\n" +
                        "</q1:inventory>\n" +
                    "</q1:InventoryFeed>\n";
    
                string pattern1 = @"<[^/][^:]+:";
                string output = Regex.Replace(input, pattern1, "<");
                string pattern2 = @"</[^:]+:";
                output = Regex.Replace(output, pattern2, "</");
    
                //using xml linq
                XElement element = XElement.Parse(input);
                foreach (var node in element.DescendantNodesAndSelf())
                {
                    if (node.NodeType == XmlNodeType.Element)
                    {
                        ((XElement)node).Name = ((XElement)node).Name.LocalName;
                    }
                }
            }
        }
    }
    ​
    
    0 讨论(0)
提交回复
热议问题