簡體   English   中英

使用XmlSerializer的默認xml名稱空間,但不在根目錄上

[英]Default xml namespace but not on root using XmlSerializer

因此,我正在使用XmlSerializer並使用自定義類。 元素之一需要一個默認的名稱空間,也需要它自己的值。 但是,默認名稱空間必須是動態的。 因此,雅典是該location元素的名稱空間。 下一個位置元素可能是倫敦。 例如:

<items>
    <item>
         <location xmlns="Athens">True</location>
         <location xmlns="London">False</location>
    </item>
<items>

我嘗試使用XmlSerializerNamespaces,但不允許我為第一個參數使用空字符串。 我看到的所有示例都是針對根元素的。 我希望它可用於以下課程設置。

[XmlRoot("item")]
public class Item
{
     [XmlElement("location")]
     public string Location { get;set; }
}

您也可以為元素指定名稱空間,請嘗試此操作

[XmlRoot("item")] public class Item { [XmlElement(ElementName = "location", Namespace="Athens")] public string Location { get;set; } }

您可以在類中嵌入一個XElement值屬性,該屬性根據需要使用必需的名稱空間和值進行填充,然后使用XmlAnyElementAttribute屬性對其進行XmlAnyElementAttribute以告知XmlSerializer將其原樣包含在最終XML中:

[XmlRoot("item" /*, Namespace = "" */)] // Applies when an Item is the document root.
[XmlType("item" /*, Namespace = "" */)] // Applies when an Item is not the document root.
public class Item
{
    [XmlIgnore]
    public string Location { get; set; }

    [XmlIgnore]
    public string LocationNamespace { get; set; }

    [XmlAnyElement]
    public XElement LocationElement
    {
        get
        {
            var ns = (XNamespace)LocationNamespace;
            var element = new XElement(ns + "location", Location);
            return element;
        }
        set
        {
            if (value == null)
            {
                LocationNamespace = Location = null;
            }
            else
            {
                LocationNamespace = value.Name.Namespace.NamespaceName;
                Location = value.Value;
            }
        }
    }
}

當(例如)放置在數組中時,以下類:

var items = new List<Item> { new Item { Location = true.ToString(), LocationNamespace = "Athens" } };

將被序列化為以下XML:

 <ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <item> <location xmlns="Athens">True</location> </item> </ArrayOfItem> 

更新資料

您更新的問題需要在每個Item包含一系列位置。 您可以通過讓XmlAnyElement返回XElements數組來實現:

public class Item
{
    public Item()
    {
        this.Locations = new List<string>();
    }

    [XmlIgnore]
    public List<string> Locations { get; set; }

    [XmlIgnore]
    public string Location { get; set; }

    [XmlAnyElement]
    public XElement [] LocationElements {
        get
        {
            var elements = Locations.Select(l => new XElement(XName.Get("location", l), (l == Location).ToString())).ToArray();
            return elements;
        }
        set
        {
            Locations = value.Select(el => el.Name.Namespace.NamespaceName).ToList();
            Location = value.Where(el => bool.Parse(el.Value)).Select(el => el.Name.Namespace.NamespaceName).Distinct().SingleOrDefault() ?? string.Empty;
        }
    }
}

然后以下

        var cities = new List<string> { "Athens", "London", "Salt Lake City" };
        var items = new List<Item> { new Item { Locations = cities, Location = cities[0] }, new Item { Locations = cities, Location = cities[1] } };

產生以下XML:

 <ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <item> <location xmlns="Athens">True</location> <location xmlns="London">False</location> <location xmlns="Salt Lake City">False</location> </item> <item> <location xmlns="Athens">False</location> <location xmlns="London">True</location> <location xmlns="Salt Lake City">False</location> </item> </ArrayOfItem> 

暫無
暫無

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

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