C# XML 序列化 - 禁用呈现数组的根元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2006482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Serialization - Disable rendering root element of array
提问by Jan Remunda
Can I somehow disable rendering of root element of collection?
我可以以某种方式禁用集合根元素的渲染吗?
This class with serialization attributes:
此类具有序列化属性:
[XmlRoot(ElementName="SHOPITEM", Namespace="")]
public class ShopItem
{
[XmlElement("PRODUCTNAME")]
public string ProductName { get; set; }
[XmlArrayItem("VARIANT")]
public List<ShopItem> Variants { get; set; }
}
generates this XML:
生成此 XML:
<SHOPITEM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PRODUCTNAME>test</PRODUCTNAME>
<Variants>
<VARIANT>
<PRODUCTNAME>hi 1</PRODUCTNAME>
</VARIANT>
<VARIANT>
<PRODUCTNAME>hi 2</PRODUCTNAME>
</VARIANT>
</Variants>
</SHOPITEM>
I don't want <Variants>
element here. What must I do?
我不想要<Variants>
这里的元素。我必须做什么?
Also I don't need xsi and xsd namespaces in root element...
另外我不需要根元素中的 xsi 和 xsd 命名空间...
采纳答案by Rubens Farias
To disable rendering of root element of collection, you must replace the attribute [XmlArrayItem]
with [XmlElement]
in your code.
要收集的根元素的渲染禁用,则必须更换属性[XmlArrayItem]
用[XmlElement]
在你的代码。
For removing the xsi
and xsd
namespaces, create an XmlSerializerNamespaces
instance with an empty namespace and pass it when you need to serialize your object.
要删除xsi
和xsd
命名空间,请创建一个XmlSerializerNamespaces
具有空命名空间的实例,并在需要序列化对象时传递它。
Take a look on this example:
看看这个例子:
[XmlRoot("SHOPITEM")]
public class ShopItem
{
[XmlElement("PRODUCTNAME")]
public string ProductName { get; set; }
[XmlElement("VARIANT")] // was [XmlArrayItem]
public List<ShopItem> Variants { get; set; }
}
// ...
ShopItem item = new ShopItem()
{
ProductName = "test",
Variants = new List<ShopItem>()
{
new ShopItem{ ProductName = "hi 1" },
new ShopItem{ ProductName = "hi 2" }
}
};
// This will remove the xsi/xsd namespaces from serialization
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer ser = new XmlSerializer(typeof(ShopItem));
ser.Serialize(Console.Out, item, ns); // Inform the XmlSerializerNamespaces here
I got this output:
我得到了这个输出:
<?xml version="1.0" encoding="ibm850"?>
<SHOPITEM>
<PRODUCTNAME>test</PRODUCTNAME>
<VARIANT>
<PRODUCTNAME>hi 1</PRODUCTNAME>
</VARIANT>
<VARIANT>
<PRODUCTNAME>hi 2</PRODUCTNAME>
</VARIANT>
</SHOPITEM>
回答by Rob Levine
I don't believe it is possible to remove this element using the default xml serialization (with attributes). If you could do this, then serializing your ShopItem
class would result in badly formed xml (no root element) for the object, which is not allowed.
我不相信可以使用默认的 xml 序列化(带有属性)删除此元素。如果您可以这样做,那么序列化您的ShopItem
类将导致对象的 xml 格式错误(没有根元素),这是不允许的。
What you can do however, is manually implement IXmlSerializable
. This will give you the sort of fine-grained control you a re after.
但是,您可以做的是手动实现IXmlSerializable
. 这将为您提供所需的细粒度控制。
[Edit] - sorry - misread that you were trying to remove Variants, not SHOPITEM. To remove the List "outer" element, just mark it up with an [XmlElement] attribute rather than an [XmlArrayItem] attribute. This will cause the list entries to just use the specified element name, without wrapping the list in an outer element.
[编辑] - 抱歉 - 误读了您正在尝试删除变体,而不是 SHOPITEM。要删除列表“外部”元素,只需使用 [XmlElement] 属性而不是 [XmlArrayItem] 属性对其进行标记。这将导致列表条目仅使用指定的元素名称,而不会将列表包装在外部元素中。
For removing the namespaces, this is controlled by the seriliazer itself, not the markup on the class. I've just noticed that while I've updated this answer, Rubens Farias has provided an reply that shows you how to eliminate the namespace.
为了删除命名空间,这由序列化器本身控制,而不是类上的标记。我刚刚注意到,虽然我更新了这个答案,但 Rubens Farias 提供了一个回复,向您展示了如何消除命名空间。
回答by Pent Ploompuu
Replace [XmlArrayItem("VARIANT")]
with [XmlElement("VARIANT")]
.
替换[XmlArrayItem("VARIANT")]
为[XmlElement("VARIANT")]
。