清洁XML序列化的层次,递归数据结构 [英] Clean XML serializing a hierarchical, recursive data structure

查看:142
本文介绍了清洁XML序列化的层次,递归数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类:

[XmlRoot("menuItem")]
public class MenuItem
{
    [XmlAttribute("text")]
    public string Text { get; set; }

    [XmlAttribute("isLink")]
    public bool IsLink { get; set; }

    [XmlAttribute("url")]
    public string Url { get; set; }

    [XmlArray("items", IsNullable = true)]
    public List<MenuItem> Items { get; set; }
}

它定义了菜单的层次结构。现在,在序列化这个类,输出XML为3级菜单是:

Which defines a menu hierarchy. Now, on serializing this class, the output XML for a 3-level menu is:

<menuItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
text="Tools" isLink="false">
  <items>
    <MenuItem text="Market" isLink="false">
      <items>
        <MenuItem text="Market Analyzer" isLink="true" url="/tools/market/analyzer">
          <items xsi:nil="true" />
        </MenuItem>
      </items>
    </MenuItem>
    <MenuItem text="Banking" isLink="false">
      <items>
        <MenuItem text="Purchase" isLink="true" url="/buy?type=good">
          <items xsi:nil="true" />
        </MenuItem>
      </items>
    </MenuItem>
    <MenuItem text="General" isLink="false">
      <items>
        <MenuItem text="Forecasts" isLink="true" url="/wheather-forcasts?city=la">
          <items xsi:nil="true" />
        </MenuItem>
      </items>
    </MenuItem>
  </items>
</menuItem>

因此​​,菜单项既是子元素。为根,它的序列为菜单项适当的外壳。然而,作为子元素,它的资本是不正确的。我怎样才能使儿童项目输出串行器创建菜单项,而不是菜单项。区分大小写关系到我这里。

So, MenuItem is both the root and the child-element. As the root, it's serialized as menuItem with proper casing. However, as child elements, it's capitalization is not correct. How can I make the serializer create menuItem and not MenuItem in the output for child items. Case sensitivity matters to me here.

我试图把 [的XmlElement] 属性的类本身,而且得到了以下错误:

I tried to put [XmlElement] attribute on the class itself, but got the following error:

属性XmlArrayItem'不在此声明类型的有效。它是   仅适用于属性,索引,现场,参数,回报的声明。

Attribute 'XmlArrayItem' is not valid on this declaration type. It is only valid on 'property, indexer, field, param, return' declarations.

另外,我不希望这些默认的命名空间,而我不希望子项创建为空元素。最终的XML文件应尽可能干净,这个XML例子:

Also, I don't want those default namespaces there, and I don't want the child items to be created as empty elements. The ultimate XML file should be as clean as this XML example:

<menuItem text='Tools' isLink='false'>
  <items>
    <menuItem text='Market' isLink='false'>
      <items>
        <menuItem text='Market Analyzer' isLink='true' url='/tools/market/analyzer' />
      </items>
    </menuItem>
    <menuItem text='Banking' isLink='false'>
      <items>
        <menuItem text='Purchase' isLink='true' url='/buy?type=good' />
      </items>
    </menuItem>
    <menuItem text='General' isLink='false'>
      <items>
        <menuItem text='Forecasts' isLink='true' url='/wheather-forcasts?city=la' />
      </items>
    </menuItem>
  </items>
</menuItem>

属性,我应该使用什么?

What attributes should I use?

推荐答案

添加 XmlArrayItemAttribute 并带走了ISNULLABLE:

Add XmlArrayItemAttribute and take away the IsNullable:

[XmlArray("items"), XmlArrayItem("menuItem")]
public List<MenuItem> Items { get; set; }

要摆脱多余的命名空间,你需要使用 XmlSerializerNamespaces

To get rid of the extra namespaces, you need to use XmlSerializerNamespaces:

var ns = new XmlSerializerNamespaces();
ns.Add("","");
var ser = new XmlSerializer(typeof (MenuItem));
ser.Serialize(Console.Out, obj, ns);

这篇关于清洁XML序列化的层次,递归数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆