簡體   English   中英

刪除 Xml 序列化后的空 xmlns=“”

[英]Remove empty xmlns=“” after Xml Serialization

所以我仍然在問關於這個話題的問題:-(

So I create an object, decorate it with the Xml Serialization Attributes, from what I have seen I add an empty namespace to the xml serialization namepsace collections so as not to get the superfluous attributes I did not intend to have.

編輯:我的意思是這些屬性:

<url xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns="">

所以它給了我兩個額外的屬性。

經過進一步調查,如果我將文件的開頭更改為:**

writer.WriteStartElement("urlset","http://www.sitemaps.org/schemas/sitemap/0.9");

writer.WriteStartElement("urlset");

**然后我在 url 標記中沒有得到空的 xmlns="" 屬性。 這很好,但我確實要求根元素具有xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ,即:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

但是我仍然在序列化類型中得到一個空的xmlns=""屬性。

[XmlRoot(ElementName = "url", Namespace="")]
public class SitemapNode
{
    [XmlElement(ElementName = "loc")]
    public string Location { get; set; }
    [XmlElement(ElementName = "lastmod")]
    public DateTime LastModified { get; set; }
    [XmlElement(ElementName = "changefreq")]
    public SitemapChangeFrequency ChangeFrequency { get; set; }
    [XmlElement(ElementName = "priority")]
    public decimal Priority { get; set; }

    public SitemapNode()
    {
        Location = String.Empty;
        LastModified = DateTime.Now;
        ChangeFrequency = SitemapChangeFrequency.monthly;
        Priority = 0.5M;
    }

    public SitemapNode(string location, DateTime lastModified, SitemapChangeFrequency changeFrequency, decimal priority)
    {
        Location = location;
        LastModified = lastModified;
        ChangeFrequency = changeFrequency;
        Priority = priority;
    }
}

然后我使用以下 append 到我的 XmlWriter:

foreach (uk.co.andrewrea.SitemapNode node in List)
{
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add(String.Empty, String.Empty);
    Serializer.Serialize(Writer, node, ns);
}

這很好,除了我留下一個像這樣的 emtpy xmlns=""

<url xmlns="">

有人有什么想法嗎? 同樣,我可以使用 XmlTextWriter 和 XmlDocument 來實現這一點,但我需要使用 XmlWriter 來實現它。

任何幫助是極大的贊賞。

這有效(您只需要它們位於同一個命名空間中,並且使用命名空間 class 以便作者不會混淆):

[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;
    }
}

output 是(根據您正在尋找的評論):

    <?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>

我無法將節點插入到具有多個命名空間的現有文檔中。

無論我將命名空間設置為什么,它每次都會添加 xmlns 引用屬性,無論如何。 這打破了下游的黑匣子。

我最終通過做這樣的事情來解決這個問題。

XmlNode newNode = newDoc.SelectSingleNode(xpathQuery, manager);
newNode.Attributes.RemoveAll();   
node.ParentNode.InsertAfter(node.OwnerDocument.ImportNode(newNode, true), node);

您是否嘗試過不在 XmlRoot 屬性中指定命名空間?

IE:

[XmlRoot(ElementName = "url")]
public class SitemapNode
{ 
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM