Remove empty xmlns=“” after Xml Serialization

后端 未结 3 468
渐次进展
渐次进展 2020-12-19 06:27

So I am still asking questions about this topic :-(

So I create an object, decorate it with the Xml Serialization Attributes, from what I have seen I add an empty na

相关标签:
3条回答
  • 2020-12-19 06:45

    This works (you just need them to be in the same namespace and you use the namespaces class so the writter doesn't confuse):

    [TestMethod]
    public void TestMethod3()
    {
        var list = new []{new SitemapNode("1", DateTime.Now, 1), new SitemapNode("2", DateTime.Now.AddDays(1), 2)};
        var serializer = new XmlSerializer(typeof(SitemapNode));
        var st = new MemoryStream();
        using (var writer = XmlWriter.Create(st))
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "test");
            writer.WriteStartElement("test", "test");
            foreach (SitemapNode node in list)
            {
                serializer.Serialize(writer, node, ns);
            }
            writer.WriteEndElement();
        }
        st.Position = 0;
        TestContext.WriteLine(new StreamReader(st).ReadToEnd());
    }
    
    
    [XmlRoot(ElementName = "url", Namespace = "test")]
    public class SitemapNode
    {
        [XmlElement(ElementName = "loc")]
        public string Location { get; set; }
        [XmlElement(ElementName = "lastmod")]
        public DateTime LastModified { get; set; }
        [XmlElement(ElementName = "priority")]
        public decimal Priority { get; set; }
    
        public SitemapNode()
        {
            Location = String.Empty;
            LastModified = DateTime.Now;
            Priority = 0.5M;
        }
    
        public SitemapNode(string location, DateTime lastModified, decimal priority)
        {
            Location = location;
            LastModified = lastModified;
            Priority = priority;
        }
    }
    

    And the output is (based on your comments that is what you were looking for):

        <?xml version="1.0" encoding="utf-8"?><test xmlns="test">
    <url><loc>1</loc><lastmod>2009-03-05T13:35:54.6468-07:00</lastmod><priority>1</priority></url>
    <url><loc>2</loc><lastmod>2009-03-06T13:35:54.6478-07:00</lastmod><priority>2</priority></url></test>
    
    0 讨论(0)
  • 2020-12-19 06:48

    I was having trouble inserting a node into an existing document with multiple namespaces.

    No matter what I set the namespace to it would add the xmlns reference attribute every time no matter what. This was breaking something black boxed downstream.

    I eventually got around this by doing something like this.

    XmlNode newNode = newDoc.SelectSingleNode(xpathQuery, manager);
    newNode.Attributes.RemoveAll();   
    node.ParentNode.InsertAfter(node.OwnerDocument.ImportNode(newNode, true), node);
    
    0 讨论(0)
  • 2020-12-19 06:49

    Have you tried not specifying the namespace in your XmlRoot attribute?

    I.e.:

    [XmlRoot(ElementName = "url")]
    public class SitemapNode
    { 
    ...
    }
    
    0 讨论(0)
提交回复
热议问题